Skip to content

Commit 43b8461

Browse files
committed
http handler tests
Signed-off-by: Pavol Loffay <ploffay@redhat.com>
1 parent c5032b8 commit 43b8461

File tree

3 files changed

+63
-21
lines changed

3 files changed

+63
-21
lines changed

cmd/collector/app/zipkin/http_handler.go

+6
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ func (aH *APIHandler) saveSpansV2(w http.ResponseWriter, r *http.Request) {
119119
return
120120
}
121121

122+
contentType := r.Header.Get("Content-Type")
123+
if contentType != "application/json" {
124+
http.Error(w, "Unsupported Content-Type", http.StatusBadRequest)
125+
return
126+
}
127+
122128
var spans models.ListOfSpans
123129
if err = swag.ReadJSON(bodyBytes, &spans); err != nil {
124130
http.Error(w, fmt.Sprintf(app.UnableToReadBodyErrFormat, err), http.StatusBadRequest)

cmd/collector/app/zipkin/http_handler_test.go

+47-11
Original file line numberDiff line numberDiff line change
@@ -103,20 +103,13 @@ func waitForSpans(t *testing.T, handler *mockZipkinHandler, expecting int) {
103103
}
104104

105105
func TestThriftFormat(t *testing.T) {
106-
server, handler := initializeTestServer(nil)
106+
server, _ := initializeTestServer(nil)
107107
defer server.Close()
108-
109108
bodyBytes := zipkinSerialize([]*zipkincore.Span{{}})
110109
statusCode, resBodyStr, err := postBytes(server.URL+`/api/v1/spans`, bodyBytes, createHeader("application/x-thrift"))
111110
assert.NoError(t, err)
112111
assert.EqualValues(t, http.StatusAccepted, statusCode)
113112
assert.EqualValues(t, "", resBodyStr)
114-
115-
handler.zipkinSpansHandler.(*mockZipkinHandler).err = fmt.Errorf("Bad times ahead")
116-
statusCode, resBodyStr, err = postBytes(server.URL+`/api/v1/spans`, bodyBytes, createHeader("application/x-thrift"))
117-
assert.NoError(t, err)
118-
assert.EqualValues(t, http.StatusInternalServerError, statusCode)
119-
assert.EqualValues(t, "Cannot submit Zipkin batch: Bad times ahead\n", resBodyStr)
120113
}
121114

122115
func TestJsonFormat(t *testing.T) {
@@ -229,9 +222,52 @@ func TestCannotReadBodyFromRequest(t *testing.T) {
229222
req, err := http.NewRequest(http.MethodPost, "whatever", &errReader{})
230223
assert.NoError(t, err)
231224
rw := dummyResponseWriter{}
232-
handler.saveSpans(&rw, req)
233-
assert.EqualValues(t, http.StatusInternalServerError, rw.myStatusCode)
234-
assert.EqualValues(t, "Unable to process request body: Simulated error reading body\n", rw.myBody)
225+
226+
tests := []struct {
227+
handler func(w http.ResponseWriter, r *http.Request)
228+
}{
229+
{handler: handler.saveSpans},
230+
{handler: handler.saveSpansV2},
231+
}
232+
for _, test := range tests {
233+
test.handler(&rw, req)
234+
assert.EqualValues(t, http.StatusInternalServerError, rw.myStatusCode)
235+
assert.EqualValues(t, "Unable to process request body: Simulated error reading body\n", rw.myBody)
236+
}
237+
}
238+
239+
func TestSaveSpansV2(t *testing.T) {
240+
server, handler := initializeTestServer(nil)
241+
defer server.Close()
242+
tests := []struct {
243+
body []byte
244+
resBody string
245+
code int
246+
headers map[string]string
247+
}{
248+
{body: []byte("[]"), code: http.StatusAccepted},
249+
{body: gzipEncode([]byte("[]")), code: http.StatusAccepted, headers: map[string]string{"Content-Encoding": "gzip"}},
250+
{body: []byte("[]"), code: http.StatusBadRequest, headers: map[string]string{"Content-Type": "text/html"}, resBody: "Unsupported Content-Type\n"},
251+
{body: []byte("[]"), code: http.StatusBadRequest, headers: map[string]string{"Content-Encoding": "gzip"}, resBody: "Unable to process request body: unexpected EOF\n"},
252+
{body: []byte("not good"), code: http.StatusBadRequest, resBody: "Unable to process request body: invalid character 'o' in literal null (expecting 'u')\n"},
253+
{body: []byte("[{}]"), code: http.StatusBadRequest, resBody: "Unable to process request body: validation failure list:\nid in body is required\ntraceId in body is required\n"},
254+
{body: []byte(`[{"id":"1111111111111111", "traceId":"1111111111111111", "localEndpoint": {"ipv4": "A"}}]`), code: http.StatusBadRequest, resBody: "Unable to process request body: wrong ipv4\n"},
255+
}
256+
for _, test := range tests {
257+
h := createHeader("application/json")
258+
for k, v := range test.headers {
259+
h.Set(k, v)
260+
}
261+
statusCode, resBody, err := postBytes(server.URL+`/api/v2/spans`, test.body, h)
262+
require.NoError(t, err)
263+
assert.EqualValues(t, test.code, statusCode)
264+
assert.EqualValues(t, test.resBody, resBody)
265+
}
266+
handler.zipkinSpansHandler.(*mockZipkinHandler).err = fmt.Errorf("Bad times ahead")
267+
statusCode, resBody, err := postBytes(server.URL+`/api/v2/spans`, []byte(`[{"id":"1111111111111111", "traceId":"1111111111111111"}]`), createHeader("application/json"))
268+
require.NoError(t, err)
269+
assert.EqualValues(t, http.StatusInternalServerError, statusCode)
270+
assert.EqualValues(t, "Cannot submit Zipkin batch: Bad times ahead\n", resBody)
235271
}
236272

237273
type errReader struct{}

cmd/collector/app/zipkin/jsonv2_test.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@ func TestFixtures(t *testing.T) {
3636
var pid int64 = 1
3737
var ts int64 = 1
3838
var d int64 = 10
39-
localE := &zipkincore.Endpoint{ServiceName:"foo", Ipv4: 170594602}
40-
remoteE := &zipkincore.Endpoint{ServiceName:"bar", Ipv4: 170594603}
41-
tSpan := &zipkincore.Span{ID: 2, TraceID: 1, ParentID: &pid, Name:"foo", Debug: true, Duration: &d, Timestamp: &ts,
42-
Annotations:[]*zipkincore.Annotation{
43-
{Value:"foo", Timestamp:1, Host:localE},
44-
{Value: zipkincore.CLIENT_SEND, Timestamp: ts, Host:localE},
45-
{Value: zipkincore.CLIENT_RECV, Timestamp: ts+d, Host: localE}},
46-
BinaryAnnotations:[]*zipkincore.BinaryAnnotation{
47-
{Key:"foo", Value:[]byte("bar"), Host:localE, AnnotationType:zipkincore.AnnotationType_STRING},
48-
{Key:zipkincore.SERVER_ADDR, Host:remoteE, AnnotationType:zipkincore.AnnotationType_BOOL}}}
39+
localE := &zipkincore.Endpoint{ServiceName: "foo", Ipv4: 170594602}
40+
remoteE := &zipkincore.Endpoint{ServiceName: "bar", Ipv4: 170594603}
41+
tSpan := &zipkincore.Span{ID: 2, TraceID: 1, ParentID: &pid, Name: "foo", Debug: true, Duration: &d, Timestamp: &ts,
42+
Annotations: []*zipkincore.Annotation{
43+
{Value: "foo", Timestamp: 1, Host: localE},
44+
{Value: zipkincore.CLIENT_SEND, Timestamp: ts, Host: localE},
45+
{Value: zipkincore.CLIENT_RECV, Timestamp: ts + d, Host: localE}},
46+
BinaryAnnotations: []*zipkincore.BinaryAnnotation{
47+
{Key: "foo", Value: []byte("bar"), Host: localE, AnnotationType: zipkincore.AnnotationType_STRING},
48+
{Key: zipkincore.SERVER_ADDR, Host: remoteE, AnnotationType: zipkincore.AnnotationType_BOOL}}}
4949
assert.Equal(t, tSpans[0], tSpan)
5050
}
5151

0 commit comments

Comments
 (0)