forked from manyminds/api2go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_create_test.go
56 lines (50 loc) · 1.36 KB
/
api_create_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
package api2go
import (
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
)
type emptyModel struct {
}
func (e emptyModel) GetID() string {
return ""
}
func (e *emptyModel) SetID(s string) error {
return nil
}
type acceptedSource struct {}
func (s *acceptedSource) Create(obj interface{}, req Request) (Responder, error) {
return &Response{Res: obj, Code: http.StatusAccepted}, nil
}
// Don't add the Location header to api results that are "Accepted" as the are queued and don't have a resulting
// id of the created entity.
func Test_handleCreate_doesntAddLocationHeaderForStatusAcceptedWhenNoIDReturned(t *testing.T) {
r := resource{
resourceType: reflect.TypeOf(&emptyModel{}),
name: "accepted",
source: &acceptedSource{},
api: nil,
}
w := httptest.NewRecorder()
req := httptest.NewRequest("POST", "/target", strings.NewReader(`{
"data": {
"type": "emptyModels",
"attributes": {},
"relationships": {}
}
}`))
err := r.handleCreate(&APIContext{}, w, req, "prefix", information{})
if err != nil {
t.Error("received error from handleCreate when one was not expected", err.Error())
}
resp := w.Result()
locationHeader := resp.Header["Location"]
if len(locationHeader) != 0 {
t.Error("Location header should be blank")
}
if resp.StatusCode != http.StatusAccepted {
t.Error("Wrong status code received")
}
}