|
4 | 4 | "bytes" |
5 | 5 | "net/http" |
6 | 6 | "net/http/httptest" |
| 7 | + "net/url" |
| 8 | + "strconv" |
7 | 9 | "testing" |
8 | 10 | "time" |
9 | 11 |
|
@@ -45,6 +47,77 @@ func TestStreamSendsLastEventID(t *testing.T) { |
45 | 47 | assert.Equal(t, lastID, r0.Request.Header.Get("Last-Event-ID")) |
46 | 48 | } |
47 | 49 |
|
| 50 | +func TestCanReplaceStreamQueryParameters(t *testing.T) { |
| 51 | + streamHandler, streamControl := httphelpers.SSEHandler(nil) |
| 52 | + defer streamControl.Close() |
| 53 | + handler, requestsCh := httphelpers.RecordingHandler(streamHandler) |
| 54 | + |
| 55 | + httpServer := httptest.NewServer(handler) |
| 56 | + defer httpServer.Close() |
| 57 | + |
| 58 | + option := StreamOptionDynamicQueryParams(func(existing url.Values) url.Values { |
| 59 | + return url.Values{ |
| 60 | + "filter": []string{"my-custom-filter"}, |
| 61 | + "basis": []string{"last-known-basis"}, |
| 62 | + } |
| 63 | + }) |
| 64 | + |
| 65 | + stream := mustSubscribe(t, httpServer.URL, option) |
| 66 | + defer stream.Close() |
| 67 | + |
| 68 | + r0 := <-requestsCh |
| 69 | + assert.Equal(t, "my-custom-filter", r0.Request.URL.Query().Get("filter")) |
| 70 | + assert.Equal(t, "last-known-basis", r0.Request.URL.Query().Get("basis")) |
| 71 | +} |
| 72 | + |
| 73 | +func TestCanUpdateStreamQueryParameters(t *testing.T) { |
| 74 | + streamHandler, streamControl := httphelpers.SSEHandler(nil) |
| 75 | + defer streamControl.Close() |
| 76 | + handler, requestsCh := httphelpers.RecordingHandler(streamHandler) |
| 77 | + |
| 78 | + httpServer := httptest.NewServer(handler) |
| 79 | + defer httpServer.Close() |
| 80 | + |
| 81 | + option := StreamOptionDynamicQueryParams(func(existing url.Values) url.Values { |
| 82 | + if existing.Has("count") { |
| 83 | + count, _ := strconv.Atoi(existing.Get("count")) |
| 84 | + |
| 85 | + if count == 1 { |
| 86 | + existing.Set("count", strconv.Itoa(count+1)) |
| 87 | + return existing |
| 88 | + } |
| 89 | + |
| 90 | + return url.Values{} |
| 91 | + } |
| 92 | + |
| 93 | + return url.Values{ |
| 94 | + "initial": []string{"payload is set"}, |
| 95 | + "count": []string{"1"}, |
| 96 | + } |
| 97 | + }) |
| 98 | + |
| 99 | + stream := mustSubscribe(t, httpServer.URL, option, StreamOptionInitialRetry(time.Millisecond)) |
| 100 | + defer stream.Close() |
| 101 | + |
| 102 | + r0 := <-requestsCh |
| 103 | + assert.Equal(t, "payload is set", r0.Request.URL.Query().Get("initial")) |
| 104 | + assert.Equal(t, "1", r0.Request.URL.Query().Get("count")) |
| 105 | + |
| 106 | + streamControl.EndAll() |
| 107 | + <-stream.Errors // Accept the error to unblock the retry handler |
| 108 | + |
| 109 | + r1 := <-requestsCh |
| 110 | + assert.Equal(t, "payload is set", r1.Request.URL.Query().Get("initial")) |
| 111 | + assert.Equal(t, "2", r1.Request.URL.Query().Get("count")) |
| 112 | + |
| 113 | + streamControl.EndAll() |
| 114 | + <-stream.Errors // Accept the error to unblock the retry handler |
| 115 | + |
| 116 | + r2 := <-requestsCh |
| 117 | + assert.False(t, r2.Request.URL.Query().Has("initial")) |
| 118 | + assert.False(t, r2.Request.URL.Query().Has("count")) |
| 119 | +} |
| 120 | + |
48 | 121 | func TestStreamReconnectWithRequestBodySendsBodyTwice(t *testing.T) { |
49 | 122 | body := []byte("my-body") |
50 | 123 |
|
|
0 commit comments