-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathserver_response_test.go
183 lines (152 loc) · 5.88 KB
/
server_response_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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright 2021-2024 Nokia
// Licensed under the BSD 3-Clause License.
// SPDX-License-Identifier: BSD-3-Clause
package restful
import (
"context"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSendResponse(t *testing.T) {
assert := assert.New(t)
// Server
srv := httptest.NewServer(Logger(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Location", "https://me/path")
var a structType
a.Str = "hello"
SendJSONResponse(w, 201, &a, true)
})))
defer srv.Close()
var a strType
resp, err := NewClient().SendRecv2xx(context.Background(), http.MethodGet, srv.URL, nil, nil, &a)
assert.Nil(err)
assert.Equal(http.StatusCreated, resp.StatusCode)
assert.Equal("application/json", resp.Header.Get("Content-type"))
location, err := resp.Location()
assert.Nil(err)
assert.NotNil(location)
assert.Equal("https://me/path", location.String())
assert.Equal("hello", a.Str)
assert.Equal(int64(15), resp.ContentLength)
}
func TestSendEmptyResponse(t *testing.T) {
assert := assert.New(t)
// Server
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Location", "https://me")
SendEmptyResponse(w, 200)
}))
defer srv.Close()
resp, err := NewClient().SendRecv2xx(context.Background(), http.MethodGet, srv.URL, nil, nil, nil)
assert.Nil(err)
assert.Equal(http.StatusOK, resp.StatusCode)
assert.Equal("https://me", resp.Header.Get("Location"))
assert.Equal(int64(0), resp.ContentLength)
}
func TestSendLocationResponse(t *testing.T) {
assert := assert.New(t)
// Server
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
SendLocationResponse(w, "https://me")
}))
defer srv.Close()
resp, err := NewClient().SendRecv2xx(context.Background(), http.MethodGet, srv.URL, nil, nil, nil)
assert.Nil(err)
assert.Equal(http.StatusCreated, resp.StatusCode)
assert.Equal("https://me", resp.Header.Get("Location"))
assert.Equal(int64(0), resp.ContentLength)
}
func TestSendRespEmbeddedError(t *testing.T) {
assert := assert.New(t)
// Server
srv := httptest.NewServer(Logger(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err := NewError(errors.New("embedded"), 400, "error")
SendResp(w, r, err, nil)
})))
defer srv.Close()
err := NewClient().Get(context.Background(), srv.URL, nil)
assert.Equal(`{"detail":"error: embedded"}`, err.Error())
}
func TestSendRespCustomError(t *testing.T) {
assert := assert.New(t)
// Server
srv := httptest.NewServer(Logger(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
invalidParam := InvalidParam{Param: "name", Reason: "missing"}
body, err := json.Marshal(invalidParam)
assert.NoError(err)
assert.Equal(ContentTypeApplicationJSON, r.Header.Get("Accept"))
err = NewErrorWithBody(nil, http.StatusBadRequest, ContentTypeApplicationJSON, body)
assert.True(acceptContentType(r, ContentTypeApplicationJSON))
SendResp(w, r, err, nil)
assert.Equal(ContentTypeApplicationJSON, w.Header().Get("content-type"))
})))
defer srv.Close()
err := NewClient().Get(context.Background(), srv.URL, nil)
assert.Equal(http.StatusBadRequest, GetErrStatusCode(err))
contentType, body := GetErrBody(err)
assert.Equal(ContentTypeApplicationJSON, contentType)
assert.Equal(`{"param":"name","reason":"missing"}`, string(body))
}
func TestSendRespCustomErrorCTAny(t *testing.T) {
assert := assert.New(t)
// Server
srv := httptest.NewServer(Logger(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
invalidParam := InvalidParam{Param: "name", Reason: "missing"}
body, err := json.Marshal(invalidParam)
assert.NoError(err)
r.Header.Set(AcceptHeader, ContentTypeAny)
err = NewErrorWithBody(nil, http.StatusBadRequest, ContentTypeApplicationJSON, body)
assert.True(acceptContentType(r, ContentTypeApplicationJSON))
SendResp(w, r, err, nil)
assert.Equal(ContentTypeApplicationJSON, w.Header().Get("content-type"))
})))
defer srv.Close()
err := NewClient().Get(context.Background(), srv.URL, nil)
assert.Equal(http.StatusBadRequest, GetErrStatusCode(err))
contentType, body := GetErrBody(err)
assert.Equal(ContentTypeApplicationJSON, contentType)
assert.Equal(`{"param":"name","reason":"missing"}`, string(body))
}
func TestSendRespCustomErrorWithoutAccept(t *testing.T) {
assert := assert.New(t)
// Server
srv := httptest.NewServer(Logger(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
invalidParam := InvalidParam{Param: "name", Reason: "missing"}
body, err := json.Marshal(invalidParam)
assert.NoError(err)
r.Header.Del("Accept")
err = NewErrorWithBody(nil, http.StatusBadRequest, ContentTypeApplicationJSON, body)
assert.True(acceptContentType(r, ContentTypeApplicationJSON))
SendResp(w, r, err, nil)
assert.Equal(ContentTypeApplicationJSON, w.Header().Get("content-type"))
})))
defer srv.Close()
err := NewClient().Get(context.Background(), srv.URL, nil)
assert.Equal(http.StatusBadRequest, GetErrStatusCode(err))
contentType, body := GetErrBody(err)
assert.Equal(ContentTypeApplicationJSON, contentType)
assert.Equal(`{"param":"name","reason":"missing"}`, string(body))
}
func TestSendRespCustomErrorWithoutCT(t *testing.T) {
assert := assert.New(t)
// Server
srv := httptest.NewServer(Logger(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
invalidParam := InvalidParam{Param: "name", Reason: "missing"}
body, err := json.Marshal(invalidParam)
assert.NoError(err)
err = NewErrorWithBody(nil, http.StatusBadRequest, "", body)
assert.False(acceptContentType(r, ""))
SendResp(w, r, err, nil)
assert.Equal("", w.Header().Get("content-type"))
})))
defer srv.Close()
err := NewClient().Get(context.Background(), srv.URL, nil)
assert.Equal(http.StatusBadRequest, GetErrStatusCode(err))
contentType, body := GetErrBody(err)
assert.Equal("", contentType)
assert.Equal(``, string(body))
}