This repository has been archived by the owner on Dec 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfixture.go
67 lines (60 loc) · 1.53 KB
/
fixture.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 httpunit
import (
"encoding/json"
"log"
"net/http"
"net/http/httptest"
"reflect"
)
type HTTPFixture struct {
t T
DumpHandler *DumpHandler
RequestBuilder *RequestBuilder
ResponseRecorder *httptest.ResponseRecorder
}
func NewFixture(t T, inner http.Handler) *HTTPFixture {
return &HTTPFixture{
t: t,
RequestBuilder: NewRequestBuilder(),
DumpHandler: NewDumpHandler(t, inner),
ResponseRecorder: httptest.NewRecorder(),
}
}
func (this *HTTPFixture) Teardown() {
this.DumpHandler.Teardown()
}
func (this *HTTPFixture) Serve() {
this.DumpHandler.ServeHTTP(this.ResponseRecorder, this.RequestBuilder.Build())
}
func (this *HTTPFixture) DeserializeJSONResponseBody(v interface{}) {
decoder := json.NewDecoder(this.ResponseRecorder.Result().Body)
err := decoder.Decode(v)
if err != nil {
log.Panicln(err)
}
}
func (this *HTTPFixture) AssertJSONResponse(expectedStatus int, expectedBody interface{}) {
this.AssertResponseStatusCode(expectedStatus)
var actualBody interface{}
this.DeserializeJSONResponseBody(&actualBody)
if !reflect.DeepEqual(expectedBody, actualBody) {
this.reportFailure(expectedBody, actualBody)
}
}
func (this *HTTPFixture) AssertResponseStatusCode(expected int) {
actual := this.ResponseRecorder.Result().StatusCode
if actual == expected {
return
}
this.t.Helper()
this.reportFailure(expected, actual)
}
func (this *HTTPFixture) reportFailure(expected, actual interface{}) {
this.t.Helper()
this.t.Errorf("\n"+
"Expected: %#v\n"+
"Actual: %#v",
expected,
actual,
)
}