File tree 1 file changed +44
-0
lines changed
1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change
1
+ package main
2
+
3
+ import (
4
+ "fmt"
5
+ "net/http"
6
+ "net/http/httptest"
7
+ "net/url"
8
+ "strings"
9
+ "testing"
10
+ )
11
+
12
+ func TestSimple (t * testing.T ) {
13
+ ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
14
+ w .Header ().Set ("Response" , "check" )
15
+ fmt .Fprintln (w , "the response" )
16
+ }))
17
+ defer ts .Close ()
18
+
19
+ u , err := url .Parse (ts .URL )
20
+ if err != nil {
21
+ t .Fatal (err )
22
+ }
23
+
24
+ req := RawRequest {
25
+ transport : "tcp" ,
26
+ host : u .Hostname (),
27
+ port : u .Port (),
28
+ request : "GET /anything HTTP/1.1\r \n " + "Host: localhost\r \n " ,
29
+ }
30
+
31
+ resp , err := Do (req )
32
+ if err != nil {
33
+ t .Errorf ("want nil error, have %s" , err )
34
+ }
35
+
36
+ have := strings .TrimSpace (string (resp .body ))
37
+ if have != "the response" {
38
+ t .Errorf ("want body to be 'the response'; have '%s'" , have )
39
+ }
40
+
41
+ if resp .Header ("Response" ) != "check" {
42
+ t .Errorf ("want response header to be 'check' have '%s'" , resp .Header ("Response" ))
43
+ }
44
+ }
You can’t perform that action at this time.
0 commit comments