-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser_test.go
55 lines (49 loc) · 1.12 KB
/
parser_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
package gocookie_string_reader
import (
"bufio"
"encoding/json"
"fmt"
"net/http"
"reflect"
"strings"
"testing"
)
var readCookiesTests = []struct {
Header http.Header
}{
{
http.Header{"Cookie": {"Cookie-1=v$1", "c2=v2"}},
},
{
http.Header{"Cookie": {"Cookie-1=v$1", "c2=v2"}},
},
{
http.Header{"Cookie": {"Cookie-1=v$1; c2=v2"}},
},
{
http.Header{"Cookie": {"Cookie-1=v$1; c2=v2"}},
},
{
http.Header{"Cookie": {`Cookie-1="v$1"; c2="v2"`}},
},
}
func TestReadCookies(t *testing.T) {
for i, tt := range readCookiesTests {
for n := 0; n < 2; n++ { // to verify readCookies doesn't mutate its input
c := ParseToCookie(tt.Header.Get("Cookie"))
rawRequest := fmt.Sprintf("GET / HTTP/1.0\r\nCookie: %s\r\n\r\n", tt.Header.Get("Cookie"))
req, _ := http.ReadRequest(bufio.NewReader(strings.NewReader(rawRequest)))
if !reflect.DeepEqual(c, req.Cookies()) {
t.Errorf("#%d readCookies:\nhave: %s\nwant: %s\n", i, toJSON(c), toJSON(req.Cookies()))
continue
}
}
}
}
func toJSON(v interface{}) string {
b, err := json.Marshal(v)
if err != nil {
return fmt.Sprintf("%#v", v)
}
return string(b)
}