forked from sohlich/nats-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context_test.go
132 lines (111 loc) · 3.05 KB
/
context_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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package natsproxy
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"testing"
)
func TestPathVariable(t *testing.T) {
url := "/test/1234/tst?name=testuser"
req := &Request{
URL: url,
}
resp := &Response{}
ctx := newContext(buildParamMap("/test/:token/:session"), resp, req)
tkn := ctx.PathVariable("token")
if tkn != "1234" {
t.Error("Defined path variable assertion failed")
}
session := ctx.PathVariable("session")
if session != "tst" {
t.Error("Defined path variable returned empty string")
}
unknwn := ctx.PathVariable("novalue")
if unknwn != "" {
t.Error("Non existing path variable returned non empty string")
}
unknwn = ctx.PathVariable("")
if unknwn != "" {
t.Error("Non existing path variable returned non empty string")
}
url = ""
req.URL = url
tkn = ctx.PathVariable("token")
if tkn != "" {
t.Error("No variable in URL.Path returned non emtpy token")
}
}
func TestParseForm(t *testing.T) {
url := "http://127.0.0.1:3000/test/12324/123?name=queryname"
reader := strings.NewReader("z=post&both=y&prio=2&empty=&name=postname")
req, _ := http.NewRequest("POST", "http://127.0.0.1:3000/test/12324/123?name=queryname", reader)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
req.Header.Set("X-AUTH", "xauthpayload")
testRequest := NewRequest()
testRequest.FromHTTP(req)
c := newContext(buildParamMap(url), NewResponse(), testRequest)
c.ParseForm()
if c.FormVariable("name") != "postname" {
fmt.Println("Got " + c.FormVariable("name"))
t.Error("Form variable assertion failed")
}
}
func TestParseFormNilBody(t *testing.T) {
url := "http://127.0.0.1:3000/test/12324/123?name=queryname"
req, _ := http.NewRequest("POST", "http://127.0.0.1:3000/test/12324/123?name=queryname", nil)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
req.Header.Set("X-AUTH", "xauthpayload")
testRequest := NewRequest()
testRequest.FromHTTP(req)
testRequest.Body = nil
c := newContext(buildParamMap(url), NewResponse(), testRequest)
if err := c.ParseForm(); err == nil {
t.FailNow()
}
}
func TestAbortContext(t *testing.T) {
req := &Request{
URL: "/test/1234/tst",
}
resp := &Response{}
ctx := newContext(buildParamMap("/test/:token/:session"), resp, req)
ctx.Abort()
if ctx.IsAborted() != true {
t.FailNow()
}
}
func TestAbortJSONContext(t *testing.T) {
req := &Request{
URL: "/test/1234/tst",
}
resp := &Response{}
ctx := newContext(buildParamMap("/test/:token/:session"), resp, req)
ctx.AbortWithJSON("test")
if ctx.IsAborted() != true {
t.FailNow()
}
if exp, _ := json.Marshal("test"); string(exp) != string(resp.Body) {
t.FailNow()
}
}
type testStruct struct {
Data string
}
func TestBindJson(t *testing.T) {
dataStruct := testStruct{
"Test",
}
data, _ := json.Marshal(dataStruct)
req := &Request{
URL: "/test/1234/tst",
Body: data,
}
resp := &Response{}
ctx := newContext(buildParamMap("/test/:token/:session"), resp, req)
verifStruct := &testStruct{}
ctx.BindJSON(verifStruct)
if verifStruct.Data != dataStruct.Data {
t.FailNow()
}
}