-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathfull_test.go
More file actions
77 lines (63 loc) · 1.68 KB
/
full_test.go
File metadata and controls
77 lines (63 loc) · 1.68 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
package rawhttp
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
)
func TestRaw(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Response", "check")
fmt.Fprintln(w, "the response")
}))
defer ts.Close()
u, err := url.Parse(ts.URL)
if err != nil {
t.Fatal(err)
}
req := RawRequest{
Hostname: u.Hostname(),
Port: u.Port(),
Request: "GET /anything HTTP/1.1\r\n" + "Host: localhost\r\n",
}
resp, err := Do(req)
if err != nil {
t.Errorf("want nil error, have %s", err)
}
have := strings.TrimSpace(string(resp.Body()))
if have != "the response" {
t.Errorf("want body to be 'the response'; have '%s'", have)
}
if resp.Header("Response") != "check" {
t.Errorf("want response header to be 'check' have '%s'", resp.Header("Response"))
}
if resp.StatusCode() != "200" {
t.Errorf("want 200 response; have %s", resp.StatusCode())
}
}
func TestFromURL(t *testing.T) {
ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Response", "check")
fmt.Fprintln(w, "the response")
}))
defer ts.Close()
req, err := FromURL("POST", ts.URL)
if err != nil {
t.Fatalf("want nil error, have %s", err)
}
req.AutoSetHost()
req.Body = "This is some POST data"
resp, err := Do(req)
if err != nil {
t.Fatalf("want nil error, have %s", err)
}
have := strings.TrimSpace(string(resp.Body()))
if have != "the response" {
t.Errorf("want body to be 'the response'; have '%s'", have)
}
if resp.Header("Response") != "check" {
t.Errorf("want response header to be 'check' have '%s'", resp.Header("Response"))
}
}