| 8 | ) |
| 9 | |
| 10 | func TestHTTPHeaders(t *testing.T) { |
| 11 | headers := HTTPHeaders{ |
| 12 | Header: http.Header{ |
| 13 | "Content-Type": {"application/json"}, |
| 14 | "Cf-Worker": {"true"}, |
| 15 | "User-Agent": {"Go-http-client/2"}, |
| 16 | }, |
| 17 | } |
| 18 | |
| 19 | t.Run("contains-existing-header", func(t *testing.T) { |
| 20 | resp := headers.Contains(types.String("User-Agent")) |
| 21 | if !resp.(types.Bool) { |
| 22 | t.Fatal("headers does not contain User-Agent") |
| 23 | } |
| 24 | }) |
| 25 | |
| 26 | t.Run("not-contains-missing-header", func(t *testing.T) { |
| 27 | resp := headers.Contains(types.String("Xxx-Random-Header")) |
| 28 | if resp.(types.Bool) { |
| 29 | t.Fatal("headers does not contain User-Agent") |
| 30 | } |
| 31 | }) |
| 32 | |
| 33 | t.Run("get-existing-header", func(t *testing.T) { |
| 34 | val := headers.Get(types.String("User-Agent")) |
| 35 | switch val.(type) { |
| 36 | case types.String: |
| 37 | // ok |
| 38 | default: |
| 39 | t.Fatalf("result was wrong type %T", val) |
| 40 | } |
| 41 | }) |
| 42 | |
| 43 | t.Run("not-get-missing-header", func(t *testing.T) { |
| 44 | val := headers.Get(types.String("Xxx-Random-Header")) |
| 45 | switch val.(type) { |
| 46 | case *types.Err: |
| 47 | // ok |
| 48 | default: |
| 49 | t.Fatalf("result was wrong type %T", val) |
| 50 | } |
| 51 | }) |
| 52 | } |