-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpreparar_test.go
More file actions
127 lines (103 loc) · 4.61 KB
/
preparar_test.go
File metadata and controls
127 lines (103 loc) · 4.61 KB
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
package regn_test
import (
"bytes"
"strings"
"testing"
regn "github.com/xsxo/regnhttp"
)
var req *regn.RequestType
func prepare_request() {
req = regn.Request(4 * 1024)
req.SetMethod(regn.MethodPost)
req.SetURL("https://localhost:8080/api")
req.Header.Set("Message1", "REGN HTTP v0.0.0-rc @xsxo - GitHub.com")
req.Header.Set("Message2", "REGN HTTP v0.0.0-rc @xsxo - GitHub.com")
req.SetBody([]byte("REGN HTTP TEST BODY"))
}
func clear_request() {
req.Header.Del("Message1")
req.Header.Remove("Message2")
req.SetBody(nil)
}
func testMethod(t *testing.T) {
req = regn.Request(4 * 1024)
req.SetMethod(regn.MethodPut)
if raw := req.RawString(); raw[:len(regn.MethodPut)] != regn.MethodPut || raw[len(regn.MethodPut)] != ' ' {
t.Error("error when prepare RequestType.SetMethod function `set`\n" + strings.ReplaceAll(req.RawString(), "\r\n", "\\r\\n"))
}
req.SetMethod(regn.MethodConnect)
if raw := req.RawString(); raw[:len(regn.MethodConnect)] != regn.MethodConnect || raw[len(regn.MethodConnect)] != ' ' {
t.Error("error when prepare RequestType.SetMethod function `from lower to upper`\n" + strings.ReplaceAll(req.RawString(), "\r\n", "\\r\\n"))
}
req.SetMethod(regn.MethodPost)
if raw := req.RawString(); raw[:len(regn.MethodPost)] != regn.MethodPost || raw[len(regn.MethodPost)] != ' ' {
t.Error("error when prepare RequestType.SetMethod function `from upper to lower`\n" + strings.ReplaceAll(req.RawString(), "\r\n", "\\r\\n"))
}
}
func testURL(t *testing.T) {
req = regn.Request(4 * 1024)
req.SetURL("https://localhost:8080/api")
if !strings.Contains(req.RawString(), " /api ") {
t.Error("error when prepare RequestType.SetURL function `from lower to upper`\n" + strings.ReplaceAll(req.RawString(), "\r\n", "\\r\\n"))
}
req.SetURL("https://localhost:8080/")
if !strings.Contains(req.RawString(), " / ") {
t.Error("error when prepare RequestType.SetURL function `from upper to lower`\n" + strings.ReplaceAll(req.RawString(), "\r\n", "\\r\\n"))
}
}
func testHeader(t *testing.T) {
req = regn.Request(4 * 1024)
req.Header.Set("lanugage", "english")
if !strings.Contains(req.RawString(), "lanugage: english\r\n") {
t.Error("error when prepare RequestType.HeaderSet function `set`\n" + strings.ReplaceAll(req.RawString(), "\r\n", "\\r\\n"))
}
req.Header.Set("lanugage", "en")
if !strings.Contains(req.RawString(), "lanugage: en\r\n") {
t.Error("error when prepare RequestType.HeaderSet function `from upper to lower`\n" + strings.ReplaceAll(req.RawString(), "\r\n", "\\r\\n"))
}
req.Header.Set("lanugage", "_english_")
if !strings.Contains(req.RawString(), "lanugage: _english_\r\n") {
t.Error("error when prepare RequestType.HeaderSet function `from lower to upper`\n" + strings.ReplaceAll(req.RawString(), "\r\n", "\\r\\n"))
}
}
func testBody(t *testing.T) {
req = regn.Request(4 * 1024)
req.SetBodyString("hello")
if !strings.Contains(req.RawString(), "\r\n\r\nhello") {
t.Error("error when prepare RequestType.SetBodyString function `set`\n" + strings.ReplaceAll(req.RawString(), "\r\n", "\\r\\n"))
}
req.SetBodyString("h")
if !strings.Contains(req.RawString(), "\r\n\r\nh") {
t.Error("error when prepare RequestType.SetBodyString function `from upper to lower`\n" + strings.ReplaceAll(req.RawString(), "\r\n", "\\r\\n"))
}
req.SetBodyString("hello world")
if !strings.Contains(req.RawString(), "\r\n\r\nhello world") {
t.Error("error when prepare RequestType.SetBodyString function `from lower to upper`\n" + strings.ReplaceAll(req.RawString(), "\r\n", "\\r\\n"))
}
}
func Test_SetHeaders(t *testing.T) {
prepare_request()
if !bytes.Contains(req.Raw(), []byte(" /api ")) {
t.Error("error when prepare RequestType.SetURL function")
} else if !bytes.Contains(req.Raw(), []byte("Message1: ")) {
t.Error("error when prepare RequestType.Header.Set function")
} else if !bytes.Contains(req.Raw(), []byte("Message2: ")) {
t.Error("error when prepare RequestType.Header.Add function")
} else if !bytes.Contains(req.Raw(), []byte("\r\n\r\nREGN HTTP TEST BODY")) {
t.Error("error when prepare RequestType.SetBody function")
} else if !bytes.Contains(req.Raw(), []byte("Content-Length: ")) {
t.Error("error when prepare RequestType.SetBody function")
}
clear_request()
if bytes.Contains(req.Raw(), []byte("Message1: ")) {
t.Error("error when prepare RequestType.Header.Del function")
} else if bytes.Contains(req.Raw(), []byte("Message2: ")) {
t.Error("error when prepare RequestType.Header.Remove function")
} else if bytes.Contains(req.Raw(), []byte("\r\n\r\nREGN HTTP TEST BODY")) {
t.Error("error when prepare RequestType.SetBody function")
}
testMethod(t)
testURL(t)
testHeader(t)
testBody(t)
}