Skip to content

Commit

Permalink
Open the HTTP body to hint clients that all headers have been sent
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas committed Mar 21, 2019
1 parent d3283bc commit 99cb809
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
4 changes: 2 additions & 2 deletions hub/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func TestServe(t *testing.T) {
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)

assert.Equal(t, []byte("id: first\ndata: hello\n\n"), body)
assert.Equal(t, []byte(":\nid: first\ndata: hello\n\n"), body)
}()

go func() {
Expand All @@ -109,7 +109,7 @@ func TestServe(t *testing.T) {
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)

assert.Equal(t, []byte("id: first\ndata: hello\n\n"), body)
assert.Equal(t, []byte(":\nid: first\ndata: hello\n\n"), body)
}()

wgConnected.Wait()
Expand Down
4 changes: 4 additions & 0 deletions hub/subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ func sendHeaders(w http.ResponseWriter) {

// NGINX support https://www.nginx.com/resources/wiki/start/topics/examples/x-accel/#x-accel-buffering
w.Header().Set("X-Accel-Buffering", "no")

// Write a comment in the body
// Go currently doesn't provide a better way to flush the headers
fmt.Fprint(w, ":\n")
w.(http.Flusher).Flush()
}

Expand Down
12 changes: 6 additions & 6 deletions hub/subscribe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func TestSubscribe(t *testing.T) {

resp := w.Result()
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, "id: b\ndata: Hello World\n\nid: c\ndata: Great\n\n", w.Body.String())
assert.Equal(t, ":\nid: b\ndata: Hello World\n\nid: c\ndata: Great\n\n", w.Body.String())
}

func TestUnsubscribe(t *testing.T) {
Expand Down Expand Up @@ -234,7 +234,7 @@ func TestSubscribeTarget(t *testing.T) {

resp := w.Result()
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, "event: test\nid: b\ndata: Hello World\n\nretry: 1\nid: c\ndata: Great\n\n", w.Body.String())
assert.Equal(t, ":\nevent: test\nid: b\ndata: Hello World\n\nretry: 1\nid: c\ndata: Great\n\n", w.Body.String())
}

func TestSubscribeAllTargets(t *testing.T) {
Expand Down Expand Up @@ -274,7 +274,7 @@ func TestSubscribeAllTargets(t *testing.T) {

resp := w.Result()
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, "id: a\ndata: Foo\n\nevent: test\nid: b\ndata: Hello World\n\n", w.Body.String())
assert.Equal(t, ":\nid: a\ndata: Foo\n\nevent: test\nid: b\ndata: Hello World\n\n", w.Body.String())
}

func TestSendMissedEvents(t *testing.T) {
Expand Down Expand Up @@ -309,7 +309,7 @@ func TestSendMissedEvents(t *testing.T) {
defer w.Done()
req := httptest.NewRequest("GET", "http://example.com/hub?topic=http://example.com/foos/{id}&Last-Event-ID=a", nil)
hub.SubscribeHandler(wr1, req)
assert.Equal(t, "id: b\ndata: d2\n\n", wr1.Body.String())
assert.Equal(t, ":\nid: b\ndata: d2\n\n", wr1.Body.String())
}(&wg)

wr2 := newCloseNotifyingRecorder()
Expand All @@ -318,7 +318,7 @@ func TestSendMissedEvents(t *testing.T) {
req := httptest.NewRequest("GET", "http://example.com/hub?topic=http://example.com/foos/{id}", nil)
req.Header.Add("Last-Event-ID", "a")
hub.SubscribeHandler(wr2, req)
assert.Equal(t, "id: b\ndata: d2\n\n", wr2.Body.String())
assert.Equal(t, ":\nid: b\ndata: d2\n\n", wr2.Body.String())
}(&wg)

for {
Expand Down Expand Up @@ -368,7 +368,7 @@ func TestSubscribeHeartbeat(t *testing.T) {

resp := w.Result()
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, "id: b\ndata: Hello World\n\n:\n", w.Body.String())
assert.Equal(t, ":\nid: b\ndata: Hello World\n\n:\n", w.Body.String())
}

// From https://github.com/go-martini/martini/blob/master/response_writer_test.go
Expand Down

0 comments on commit 99cb809

Please sign in to comment.