-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresttest.go
63 lines (49 loc) · 2.59 KB
/
resttest.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
package resttest
import (
"io"
"net/http"
"net/http/httptest"
)
func request(HttpMethod string, routeHandler http.HandlerFunc, url string, body io.Reader) httptest.ResponseRecorder {
request, _ := http.NewRequest(HttpMethod, url, body)
responseRecorder := httptest.NewRecorder()
handler := http.HandlerFunc(routeHandler)
handler.ServeHTTP(responseRecorder, request)
return *responseRecorder
}
// RequestGet emulates GET HTTP request with route handler and return response
func RequestGet(routeHandler http.HandlerFunc, url string, body io.Reader) httptest.ResponseRecorder {
return request(http.MethodGet, routeHandler, url, body)
}
// RequestPost emulates POST HTTP request with route handler and return response
func RequestPost(routeHandler http.HandlerFunc, url string, body io.Reader) httptest.ResponseRecorder {
return request(http.MethodPost, routeHandler, url, body)
}
// RequestPatch emulates PATCH HTTP request with route handler and return response
func RequestPatch(routeHandler http.HandlerFunc, url string, body io.Reader) httptest.ResponseRecorder {
return request(http.MethodPatch, routeHandler, url, body)
}
// RequestPut emulates PUT HTTP request with route handler and return response
func RequestPut(routeHandler http.HandlerFunc, url string, body io.Reader) httptest.ResponseRecorder {
return request(http.MethodPut, routeHandler, url, body)
}
// RequestDelete emulates DELETE HTTP request with route handler and return response
func RequestDelete(routeHandler http.HandlerFunc, url string, body io.Reader) httptest.ResponseRecorder {
return request(http.MethodDelete, routeHandler, url, body)
}
// RequestHead emulates HEAD HTTP request with route handler and return response
func RequestHead(routeHandler http.HandlerFunc, url string, body io.Reader) httptest.ResponseRecorder {
return request(http.MethodHead, routeHandler, url, body)
}
// RequestOptions emulates OPTIONS HTTP request with route handler and return response
func RequestOptions(routeHandler http.HandlerFunc, url string, body io.Reader) httptest.ResponseRecorder {
return request(http.MethodOptions, routeHandler, url, body)
}
// RequestConnect emulates CONNECT HTTP request with route handler and return response
func RequestConnect(routeHandler http.HandlerFunc, url string, body io.Reader) httptest.ResponseRecorder {
return request(http.MethodConnect, routeHandler, url, body)
}
// RequestTrace emulates TRACE HTTP request with route handler and return response
func RequestTrace(routeHandler http.HandlerFunc, url string, body io.Reader) httptest.ResponseRecorder {
return request(http.MethodTrace, routeHandler, url, body)
}