-
Notifications
You must be signed in to change notification settings - Fork 5
/
handlers.go
108 lines (85 loc) · 2.82 KB
/
handlers.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
package testutils
import (
"encoding/json"
"fmt"
"io"
"net/http"
"reflect"
"testing"
)
// HandleReqOpts represents options for the testing utils package handlers.
type HandleReqOpts struct {
// Mux represents HTTP Mux for a testing handler.
Mux *http.ServeMux
// URL represents handler's HTTP URL.
URL string
// RawResponse represents raw string HTTP response that needs to be returned
// by the handler.
RawResponse string
// RawRequest represents raw string HTTP request that needs to be compared
// with the actual request that will be provided by the caller.
RawRequest string
// Method contains HTTP method that needs to be compared against real method
// provided by the caller.
Method string
// Status represents HTTP status that will be returned by the handler.
Status int
// CallFlag can be used to check if caller sent a request to a handler.
CallFlag *bool
}
// HandleReqWithoutBody provides the HTTP endpoint to test requests without body.
func HandleReqWithoutBody(t *testing.T, opts *HandleReqOpts) {
opts.Mux.HandleFunc(opts.URL, func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(opts.Status)
fmt.Fprint(w, opts.RawResponse)
if r.Method != opts.Method {
t.Fatalf("expected %s method but got %s", opts.Method, r.Method)
}
*opts.CallFlag = true
})
}
// HandleReqWithBody provides the HTTP endpoint to test requests with body.
func HandleReqWithBody(t *testing.T, opts *HandleReqOpts) {
opts.Mux.HandleFunc(opts.URL, func(w http.ResponseWriter, r *http.Request) {
if r.Method != opts.Method {
t.Fatalf("expected %s method but got %s", opts.Method, r.Method)
}
_, contentTypeExists := r.Header["Content-Type"]
if !contentTypeExists {
t.Fatalf("request doesn't contain content-type in headers")
}
contentTypeFound := false
for _, contentTypeValue := range r.Header["Content-Type"] {
if contentTypeValue == "application/json" {
contentTypeFound = true
break
}
}
if !contentTypeFound {
t.Fatalf("content-type is not equal to application/json")
}
b, err := io.ReadAll(r.Body)
if err != nil {
t.Errorf("unable to read the request body: %v", err)
}
defer r.Body.Close()
var actualRequest interface{}
err = json.Unmarshal(b, &actualRequest)
if err != nil {
t.Errorf("unable to unmarshal the request body: %v", err)
}
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(opts.Status)
fmt.Fprint(w, opts.RawResponse)
var expectedRequest interface{}
err = json.Unmarshal([]byte(opts.RawRequest), &expectedRequest)
if err != nil {
t.Errorf("unable to unmarshal expected raw request: %v", err)
}
if !reflect.DeepEqual(expectedRequest, actualRequest) {
t.Fatalf("expected %#v request, but got %#v", expectedRequest, actualRequest)
}
*opts.CallFlag = true
})
}