|
1 | 1 | package executor |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "net/http" |
4 | 5 | "net/http/httptest" |
5 | 6 | "testing" |
6 | 7 | "time" |
@@ -47,3 +48,83 @@ func TestGetTimeout_NoDefaultMeansNoOverride(t *testing.T) { |
47 | 48 | t.Errorf("getTimeout() got: %v, want %v", got, want) |
48 | 49 | } |
49 | 50 | } |
| 51 | + |
| 52 | +func Test_requiresStdlibProxy(t *testing.T) { |
| 53 | + testCases := []struct { |
| 54 | + name string |
| 55 | + headers map[string]string |
| 56 | + want bool |
| 57 | + }{ |
| 58 | + { |
| 59 | + name: "SSE request", |
| 60 | + headers: map[string]string{"Accept": "text/event-stream"}, |
| 61 | + want: true, |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "SSE request with multiple accept values", |
| 65 | + headers: map[string]string{"Accept": "application/json, text/event-stream;q=0.9, text/plain"}, |
| 66 | + want: true, |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "NDJSON request", |
| 70 | + headers: map[string]string{"Accept": "application/x-ndjson"}, |
| 71 | + want: true, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "NDJSON request with multiple accept values", |
| 75 | + headers: map[string]string{"Accept": "text/plain, application/x-ndjson;q=0.9, application/json;q=0.8"}, |
| 76 | + want: true, |
| 77 | + }, |
| 78 | + { |
| 79 | + name: "WebSocket request", |
| 80 | + headers: map[string]string{"Upgrade": "websocket"}, |
| 81 | + want: true, |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "Regular JSON request", |
| 85 | + headers: map[string]string{"Accept": "application/json"}, |
| 86 | + want: false, |
| 87 | + }, |
| 88 | + { |
| 89 | + name: "Regular request with multiple values", |
| 90 | + headers: map[string]string{"Accept": "text/plain, application/json;q=0.9"}, |
| 91 | + want: false, |
| 92 | + }, |
| 93 | + { |
| 94 | + name: "Request without headers", |
| 95 | + headers: map[string]string{}, |
| 96 | + want: false, |
| 97 | + }, |
| 98 | + { |
| 99 | + name: "Request with non-websocket Upgrade header", |
| 100 | + headers: map[string]string{"Accept": "application/json", "Upgrade": "h2c"}, |
| 101 | + want: false, |
| 102 | + }, |
| 103 | + |
| 104 | + { |
| 105 | + name: "Case insensitive headers", |
| 106 | + headers: map[string]string{"Accept": "APPLICATION/X-NDJSON"}, |
| 107 | + want: true, |
| 108 | + }, |
| 109 | + } |
| 110 | + |
| 111 | + for _, tc := range testCases { |
| 112 | + t.Run(tc.name, func(t *testing.T) { |
| 113 | + req, err := http.NewRequest("GET", "/test", nil) |
| 114 | + if err != nil { |
| 115 | + t.Fatal(err) |
| 116 | + } |
| 117 | + |
| 118 | + // Set headers from test case |
| 119 | + for key, value := range tc.headers { |
| 120 | + req.Header.Set(key, value) |
| 121 | + } |
| 122 | + |
| 123 | + got := requiresStdlibProxy(req) |
| 124 | + |
| 125 | + if got != tc.want { |
| 126 | + t.Errorf("Want %t, got %t", tc.want, got) |
| 127 | + } |
| 128 | + }) |
| 129 | + } |
| 130 | +} |
0 commit comments