-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_suite_test.go
85 lines (67 loc) · 1.64 KB
/
client_suite_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright (c) 2020 Pieoneers Software Incorporated. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package client_test
import (
"github.com/gin-gonic/gin"
"github.com/pieoneers/jsonapi-go"
"net/http"
"net/http/httptest"
"testing"
. "github.com/pieoneers/jsonapi-client-go"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func TestJSONAPIClient(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "JSONAPI Client Suite")
}
var (
ts *httptest.Server
client *Client
)
var _ = BeforeSuite(func() {
InitTemplates()
router := gin.Default()
router.GET("/books", func(c *gin.Context) {
body, _ := Template("books", Books{
{
ID: "1",
Title: "An Introduction to Programming in Go",
Year: "2012",
},
{
ID: "2",
Title: "Introducing Go",
Year: "2016",
},
})
c.Data(http.StatusOK, jsonapi.ContentType, body.Bytes())
})
router.POST("/books/successful", func(c *gin.Context) {
body, _ := Template("book", Book{
ID: "1",
Title: "An Introduction to Programming in Go",
Year: "2012",
})
c.Data(http.StatusCreated, jsonapi.ContentType, body.Bytes())
})
router.POST("/books/unsuccessful", func(c *gin.Context) {
body, _ := Template("errors", []*jsonapi.ErrorObject{
{
Title: "is required",
Source: jsonapi.ErrorObjectSource{
Pointer: "/data/attributes/title",
},
},
})
c.Data(http.StatusBadRequest, jsonapi.ContentType, body.Bytes())
})
ts = httptest.NewServer(router)
client = NewClient(Config{
BaseURL: ts.URL,
})
})
var _ = AfterSuite(func() {
ts.Close()
})