-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest_test.go
102 lines (73 loc) · 2.54 KB
/
request_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
package geh
import (
"bytes"
"encoding/json"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"net/http"
"testing"
)
func TestRequest_GetMethod(t *testing.T) {
httpRequest, _ := http.NewRequest("PATCH", "/v1/resource/1", nil)
request := Request{}
request.SetHttpRequest(httpRequest)
assert.Equal(t, "PATCH", request.GetMethod())
}
func TestRequest_GetURI(t *testing.T) {
httpRequest, _ := http.NewRequest("PATCH", "http://localhost:8080/v1/resource/1?query=string", nil)
request := Request{}
request.SetHttpRequest(httpRequest)
assert.Equal(t, "/v1/resource/1?query=string", request.GetURI())
}
func TestRequest_GetURL(t *testing.T) {
httpRequest, _ := http.NewRequest("PATCH", "http://localhost:8080/v1/resource/1?query=string", nil)
request := Request{}
request.SetHttpRequest(httpRequest)
assert.Equal(t, "/v1/resource/1", request.GetURL())
}
func TestRequest_GetParams(t *testing.T) {
httpRequest, _ := http.NewRequest("GET", "/resource/1", nil)
httpRequest = mux.SetURLVars(httpRequest, map[string]string{
"id": "1",
})
request := Request{}
request.SetHttpRequest(httpRequest)
assert.Equal(t, "1", request.GetParams("id"))
}
func TestRequest_GetParamsIfNotExists(t *testing.T) {
httpRequest, _ := http.NewRequest("GET", "resource/1", nil)
request := Request{}
request.SetHttpRequest(httpRequest)
assert.Equal(t, "", request.GetParams("id"))
}
func TestRequest_GetQueryString(t *testing.T) {
httpRequest, _ := http.NewRequest("GET", "/?hello=world", nil)
request := Request{}
request.SetHttpRequest(httpRequest)
assert.Equal(t, "world", request.GetQueryString("hello"))
}
func TestRequest_GetQueryStringIfDoesntExist(t *testing.T) {
httpRequest, _ := http.NewRequest("GET", "/?hello=world", nil)
request := Request{}
request.SetHttpRequest(httpRequest)
assert.Equal(t, "", request.GetQueryString("hello2"))
}
func TestRequest_GetHeader(t *testing.T) {
httpRequest, _ := http.NewRequest("GET", "/?hello=world", nil)
httpRequest.Header.Set("Content-Type", "application/json")
request := Request{}
request.SetHttpRequest(httpRequest)
assert.Equal(t, "application/json", request.GetHeader("Content-Type"))
}
func TestRequest_GetBody(t *testing.T) {
data, _ := json.Marshal(map[string]string{
"hello": "world",
})
httpRequest, _ := http.NewRequest("POST", "/resource", bytes.NewBuffer(data))
httpRequest.Header.Set("Content-Type", "application/json")
request := Request{}
request.SetHttpRequest(httpRequest)
assert.Equal(t, map[string]interface{}{
"hello": "world",
}, request.GetBody(map[string]string{}))
}