-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathparser_test.go
More file actions
86 lines (69 loc) · 1.99 KB
/
parser_test.go
File metadata and controls
86 lines (69 loc) · 1.99 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
package httpfuzz
import (
"io/ioutil"
"testing"
)
func TestHTTPRequestInvalidFileReturnsError(t *testing.T) {
req, err := RequestFromFile("notfound.request")
if err == nil {
t.Fatalf("expected error")
}
if req != nil {
t.Fatalf("request returned when expected nil: %+v", req)
}
}
func TestHTTPRequestParsedCorrectlyFromFile(t *testing.T) {
req, err := RequestFromFile("./testdata/validGET.request")
if err != nil {
t.Fatalf("expected err to be nil, got %v", err)
}
if req.Method != "GET" {
t.Fatalf("expected GET, got %v", req.Method)
}
if req.Host != "localhost:8000" {
t.Fatalf("expected URL 'localhost:8000', got %v", req.Host)
}
if req.UserAgent() != "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Firefox/78.0" {
t.Fatalf("got unexpected User-Agent %v", req.UserAgent())
}
if req.Header.Get("Cache-Control") != "no-cache" {
t.Fatalf("got unexpected cache-control header")
}
}
func TestPOSTRequestBodyParsedCorrectlyFromFile(t *testing.T) {
req, err := RequestFromFile("./testdata/validPOST.request")
if err != nil {
t.Fatalf("expected err to be nil, got %v", err)
}
if req.Method != "POST" {
t.Fatalf("expected POST, got %v", req.Method)
}
if req.Host != "localhost:8000" {
t.Fatalf("expected URL 'localhost:8000', got %v", req.Host)
}
if req.UserAgent() != "PostmanRuntime/7.26.3" {
t.Fatalf("got unexpected User-Agent %v", req.UserAgent())
}
if req.Header.Get("Cache-Control") != "no-cache" {
t.Fatalf("got unexpected cache-control header")
}
body, err := ioutil.ReadAll(req.Body)
if err != nil {
t.Fatal(err)
}
// Check for truncation bug
const expectedLength = 39
stringBody := string(body)
if len(body) != expectedLength {
t.Log(stringBody)
t.Fatalf("Body is incorrect length, expected %d, got %d", expectedLength, len(body))
}
const expectedBody = `{
"name": ` + "\"`S9`\"" + `,
"os": "Android"
}
`
if stringBody != expectedBody {
t.Fatalf("Incorrect body, expected '%s', got '%s'", expectedBody, stringBody)
}
}