forked from valyala/fasthttp
-
Notifications
You must be signed in to change notification settings - Fork 3
/
header_regression_test.go
87 lines (73 loc) · 2.51 KB
/
header_regression_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
package fasthttp
import (
"bufio"
"bytes"
"fmt"
"strings"
"testing"
)
func TestIssue28ResponseWithoutBodyNoContentType(t *testing.T) {
var r Response
// Empty response without content-type
s := r.String()
if strings.Contains(s, "Content-Type") {
t.Fatalf("unexpected Content-Type found in response header with empty body: %q", s)
}
// Explicitly set content-type
r.Header.SetContentType("foo/bar")
s = r.String()
if !strings.Contains(s, "Content-Type: foo/bar\r\n") {
t.Fatalf("missing explicitly set content-type for empty response: %q", s)
}
// Non-empty response.
r.Reset()
r.SetBodyString("foobar")
s = r.String()
if !strings.Contains(s, fmt.Sprintf("Content-Type: %s\r\n", defaultContentType)) {
t.Fatalf("missing default content-type for non-empty response: %q", s)
}
// Non-empty response with custom content-type.
r.Header.SetContentType("aaa/bbb")
s = r.String()
if !strings.Contains(s, "Content-Type: aaa/bbb\r\n") {
t.Fatalf("missing custom content-type: %q", s)
}
}
func TestIssue6RequestHeaderSetContentType(t *testing.T) {
testIssue6RequestHeaderSetContentType(t, "GET")
testIssue6RequestHeaderSetContentType(t, "POST")
testIssue6RequestHeaderSetContentType(t, "PUT")
testIssue6RequestHeaderSetContentType(t, "PATCH")
}
func testIssue6RequestHeaderSetContentType(t *testing.T, method string) {
contentType := "application/json"
contentLength := 123
var h RequestHeader
h.SetMethod(method)
h.SetRequestURI("http://localhost/test")
h.SetContentType(contentType)
h.SetContentLength(contentLength)
issue6VerifyRequestHeader(t, &h, contentType, contentLength, method)
s := h.String()
var h1 RequestHeader
br := bufio.NewReader(bytes.NewBufferString(s))
if err := h1.Read(br); err != nil {
t.Fatalf("unexpected error: %s", err)
}
issue6VerifyRequestHeader(t, &h1, contentType, contentLength, method)
}
func issue6VerifyRequestHeader(t *testing.T, h *RequestHeader, contentType string, contentLength int, method string) {
if string(h.ContentType()) != contentType {
t.Fatalf("unexpected content-type: %q. Expecting %q. method=%q", h.ContentType(), contentType, method)
}
if string(h.Method()) != method {
t.Fatalf("unexpected method: %q. Expecting %q", h.Method(), method)
}
if method != "GET" {
if h.ContentLength() != contentLength {
t.Fatalf("unexpected content-length: %d. Expecting %d. method=%q", h.ContentLength(), contentLength, method)
}
} else if h.ContentLength() != 0 {
t.Fatalf("unexpected content-length for GET method: %d. Expecting 0", h.ContentLength())
}
}