|
| 1 | +package pkg |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestParseRawResponse(t *testing.T) { |
| 9 | + t.Run("valid complete response", func(t *testing.T) { |
| 10 | + raw := "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: 5\r\n\r\nhello" |
| 11 | + resp, err := ParseRawResponse([]byte(raw)) |
| 12 | + if err != nil { |
| 13 | + t.Fatalf("unexpected error: %v", err) |
| 14 | + } |
| 15 | + if resp.StatusCode != 200 { |
| 16 | + t.Fatalf("expected status 200, got %d", resp.StatusCode) |
| 17 | + } |
| 18 | + if ct := resp.Header.Get("Content-Type"); ct != "text/html" { |
| 19 | + t.Fatalf("expected Content-Type text/html, got %s", ct) |
| 20 | + } |
| 21 | + }) |
| 22 | + |
| 23 | + t.Run("nil input", func(t *testing.T) { |
| 24 | + _, err := ParseRawResponse(nil) |
| 25 | + if err == nil { |
| 26 | + t.Fatal("expected error for nil input") |
| 27 | + } |
| 28 | + }) |
| 29 | + |
| 30 | + t.Run("empty input", func(t *testing.T) { |
| 31 | + _, err := ParseRawResponse([]byte{}) |
| 32 | + if err == nil { |
| 33 | + t.Fatal("expected error for empty input") |
| 34 | + } |
| 35 | + }) |
| 36 | + |
| 37 | + t.Run("status line only no headers", func(t *testing.T) { |
| 38 | + raw := "HTTP/1.1 200 OK\r\n\r\n" |
| 39 | + resp, err := ParseRawResponse([]byte(raw)) |
| 40 | + if err != nil { |
| 41 | + t.Fatalf("unexpected error: %v", err) |
| 42 | + } |
| 43 | + if resp.StatusCode != 200 { |
| 44 | + t.Fatalf("expected status 200, got %d", resp.StatusCode) |
| 45 | + } |
| 46 | + }) |
| 47 | + |
| 48 | + t.Run("truncated status line", func(t *testing.T) { |
| 49 | + raw := "HTTP/1." |
| 50 | + _, err := ParseRawResponse([]byte(raw)) |
| 51 | + if err == nil { |
| 52 | + t.Fatal("expected error for truncated status line") |
| 53 | + } |
| 54 | + }) |
| 55 | + |
| 56 | + t.Run("redirect response no body", func(t *testing.T) { |
| 57 | + raw := "HTTP/1.1 302 Found\r\nLocation: https://example.com/new\r\n\r\n" |
| 58 | + resp, err := ParseRawResponse([]byte(raw)) |
| 59 | + if err != nil { |
| 60 | + t.Fatalf("unexpected error: %v", err) |
| 61 | + } |
| 62 | + if resp.StatusCode != 302 { |
| 63 | + t.Fatalf("expected status 302, got %d", resp.StatusCode) |
| 64 | + } |
| 65 | + if loc := resp.Header.Get("Location"); loc != "https://example.com/new" { |
| 66 | + t.Fatalf("expected Location https://example.com/new, got %s", loc) |
| 67 | + } |
| 68 | + }) |
| 69 | + |
| 70 | + t.Run("chunked transfer encoding header", func(t *testing.T) { |
| 71 | + raw := "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n" |
| 72 | + resp, err := ParseRawResponse([]byte(raw)) |
| 73 | + if err != nil { |
| 74 | + t.Fatalf("unexpected error: %v", err) |
| 75 | + } |
| 76 | + if te := resp.Header.Get("Transfer-Encoding"); te == "" { |
| 77 | + // Transfer-Encoding may be consumed by http.ReadResponse, just verify no panic |
| 78 | + } |
| 79 | + _ = resp |
| 80 | + }) |
| 81 | + |
| 82 | + t.Run("large header value", func(t *testing.T) { |
| 83 | + largeValue := strings.Repeat("A", 8192) |
| 84 | + raw := "HTTP/1.1 200 OK\r\nX-Large: " + largeValue + "\r\n\r\n" |
| 85 | + resp, err := ParseRawResponse([]byte(raw)) |
| 86 | + if err != nil { |
| 87 | + t.Fatalf("unexpected error: %v", err) |
| 88 | + } |
| 89 | + if got := resp.Header.Get("X-Large"); got != largeValue { |
| 90 | + t.Fatalf("large header value mismatch, got length %d", len(got)) |
| 91 | + } |
| 92 | + }) |
| 93 | + |
| 94 | + t.Run("invalid status code", func(t *testing.T) { |
| 95 | + raw := "HTTP/1.1 xyz OK\r\n\r\n" |
| 96 | + _, err := ParseRawResponse([]byte(raw)) |
| 97 | + if err == nil { |
| 98 | + t.Fatal("expected error for invalid status code") |
| 99 | + } |
| 100 | + }) |
| 101 | + |
| 102 | + t.Run("incomplete header no terminator", func(t *testing.T) { |
| 103 | + raw := "HTTP/1.1 200 OK\r\nContent-Type: text/html" |
| 104 | + _, err := ParseRawResponse([]byte(raw)) |
| 105 | + // Should return error or at least not panic |
| 106 | + // http.ReadResponse may or may not error on missing \r\n\r\n, |
| 107 | + // the key requirement is no panic |
| 108 | + _ = err |
| 109 | + }) |
| 110 | + |
| 111 | + t.Run("binary body", func(t *testing.T) { |
| 112 | + body := []byte{0x00, 0x01, 0x02, 0xff, 0xfe, 0xfd} |
| 113 | + raw := append([]byte("HTTP/1.1 200 OK\r\nContent-Length: 6\r\n\r\n"), body...) |
| 114 | + resp, err := ParseRawResponse(raw) |
| 115 | + if err != nil { |
| 116 | + t.Fatalf("unexpected error: %v", err) |
| 117 | + } |
| 118 | + if resp.StatusCode != 200 { |
| 119 | + t.Fatalf("expected status 200, got %d", resp.StatusCode) |
| 120 | + } |
| 121 | + }) |
| 122 | +} |
0 commit comments