forked from manyminds/api2go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_interfaces_test.go
67 lines (54 loc) · 1.39 KB
/
api_interfaces_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
package api2go
import (
"net/http"
"net/http/httptest"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
type SomeData struct {
ID string
Data string
}
func (s SomeData) GetID() string {
return s.ID
}
func (s *SomeData) SetID(ID string) error {
s.ID = ID
return nil
}
type SomeResource struct{}
func (s SomeResource) FindOne(ID string, req Request) (interface{}, error) {
return SomeData{ID: "12345", Data: "A Brezzn"}, nil
}
func (s SomeResource) Create(obj interface{}, req Request) (string, error) {
return "newID", nil
}
func (s SomeResource) Delete(ID string, req Request) error {
return nil
}
func (s SomeResource) Update(obj interface{}, req Request) error {
return nil
}
var _ = Describe("Test interface api type casting", func() {
var (
api *API
rec *httptest.ResponseRecorder
)
BeforeEach(func() {
api = NewAPI("v1")
api.AddResource(SomeData{}, SomeResource{})
rec = httptest.NewRecorder()
})
It("FindAll returns 404 for simple CRUD", func() {
req, err := http.NewRequest("GET", "/v1/someDatas", nil)
Expect(err).ToNot(HaveOccurred())
api.Handler().ServeHTTP(rec, req)
Expect(rec.Code).To(Equal(http.StatusNotFound))
})
It("Works for a normal FindOne", func() {
req, err := http.NewRequest("GET", "/v1/someDatas/12345", nil)
Expect(err).ToNot(HaveOccurred())
api.Handler().ServeHTTP(rec, req)
Expect(rec.Code).To(Equal(http.StatusOK))
})
})