| 1 |
package build |
| 2 |
|
| 3 |
import ( |
| 4 |
"compress/gzip" |
| 5 |
"io" |
| 6 |
"net/http" |
| 7 |
"testing" |
| 8 |
|
| 9 |
e "github.com/karlseguin/expect" |
| 10 |
) |
| 11 |
|
| 12 |
type RequestTests struct{} |
| 13 |
|
| 14 |
func Test_Request(t *testing.T) { |
| 15 |
e.Expectify(new(RequestTests), t) |
| 16 |
} |
| 17 |
|
| 18 |
func (_ RequestTests) SetsProtocol() { |
| 19 |
req := Request().Proto(2, 4).Request |
| 20 |
e.Expect(req.Proto).To.Equal("HTTP/2.4") |
| 21 |
e.Expect(req.ProtoMajor).To.Equal(2) |
| 22 |
e.Expect(req.ProtoMinor).To.Equal(4) |
| 23 |
} |
| 24 |
|
| 25 |
func (_ RequestTests) SetsURL() { |
| 26 |
req := Request().URLString("http://openmymind.net/test?a=1").Request |
| 27 |
e.Expect(req.URL.Host).To.Equal("openmymind.net") |
| 28 |
e.Expect(req.URL.Path).To.Equal("/test") |
| 29 |
e.Expect(req.URL.RawQuery).To.Equal("a=1") |
| 30 |
} |
| 31 |
|
| 32 |
func (_ RequestTests) SetsPath() { |
| 33 |
req := Request().Path("/hello").Request |
| 34 |
e.Expect(req.URL.Host).To.Equal("local.test") |
| 35 |
e.Expect(req.URL.Path).To.Equal("/hello") |
| 36 |
e.Expect(req.URL.RawQuery).To.Equal("") |
| 37 |
} |
| 38 |
|
| 39 |
func (_ RequestTests) SetsRawQuery() { |
| 40 |
req := Request().RawQuery("a=1&b=2").Request |
| 41 |
e.Expect(req.URL.Query()["a"][0]).To.Equal("1") |
| 42 |
e.Expect(req.URL.Query()["b"][0]).To.Equal("2") |
| 43 |
e.Expect(req.URL.RawQuery).To.Equal("a=1&b=2") |
| 44 |
} |
| 45 |
|
| 46 |
func (_ RequestTests) SetsHost() { |
| 47 |
req := Request().Host("openmymind.io").Request |
| 48 |
e.Expect(req.URL.Host).To.Equal("openmymind.io") |
| 49 |
e.Expect(req.URL.Path).To.Equal("/spice") |
| 50 |
e.Expect(req.Host).To.Equal("openmymind.io") |
| 51 |
} |
| 52 |
|
| 53 |
func (_ RequestTests) GzipsABodyOnSettingHeader() { |
| 54 |
req := Request().Body("abc123").Header("Content-Encoding", "gzip").Request |
| 55 |
assertGzipBody(req, "abc123") |
| 56 |
} |
| 57 |
|
| 58 |
func (_ RequestTests) GzipsABodyWhenHeaderAlreadySet() { |
| 59 |
req := Request().Header("Content-Encoding", "gzip").Body("over=9000!").Request |
| 60 |
assertGzipBody(req, "over=9000!") |
| 61 |
} |
| 62 |
|
| 63 |
func assertGzipBody(req *http.Request, expected string) { |
| 64 |
reader, _ := gzip.NewReader(req.Body) |
| 65 |
actual := make([]byte, 100) |
| 66 |
read := 0 |
| 67 |
for { |
| 68 |
n, err := reader.Read(actual[read:]) |
| 69 |
read += n |
| 70 |
if err == io.EOF { |
| 71 |
break |
| 72 |
} |
| 73 |
} |
| 74 |
e.Expect(string(actual[:read])).To.Equal(expected) |
| 75 |
reader.Close() |
| 76 |
} |