Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

response_writer: support http/2 push #188

Merged
merged 4 commits into from Mar 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion response_writer.go
Expand Up @@ -16,6 +16,7 @@ package macaron

import (
"bufio"
"errors"
"fmt"
"net"
"net/http"
Expand All @@ -27,6 +28,7 @@ import (
type ResponseWriter interface {
http.ResponseWriter
http.Flusher
http.Pusher
// Status returns the status code of the response or 0 if the response has not been written.
Status() int
// Written returns whether or not the ResponseWriter has been written.
Expand Down Expand Up @@ -91,7 +93,7 @@ func (rw *responseWriter) Before(before BeforeFunc) {
func (rw *responseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
hijacker, ok := rw.ResponseWriter.(http.Hijacker)
if !ok {
return nil, nil, fmt.Errorf("the ResponseWriter doesn't support the Hijacker interface")
return nil, nil, errors.New("the ResponseWriter doesn't support the Hijacker interface")
}
return hijacker.Hijack()
}
Expand All @@ -112,3 +114,11 @@ func (rw *responseWriter) Flush() {
flusher.Flush()
}
}

func (rw *responseWriter) Push(target string, opts *http.PushOptions) error {
pusher, ok := rw.ResponseWriter.(http.Pusher)
if !ok {
return errors.New("the ResponseWriter doesn't support the Pusher interface")
}
return pusher.Push(target, opts)
}
7 changes: 7 additions & 0 deletions response_writer_test.go
Expand Up @@ -185,4 +185,11 @@ func Test_ResponseWriter(t *testing.T) {
So(resp.Code, ShouldEqual, http.StatusOK)
So(resp.Body.String(), ShouldEqual, "data: Hello\n\ndata: Hello\n\n")
})

Convey("Response writer with http/2 push", t, func() {
resp := httptest.NewRecorder()
rw := NewResponseWriter("GET", resp)
_, ok := rw.(http.Pusher)
So(ok, ShouldBeTrue)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No test case for the call of pusher.Push method?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will not lie to you. As I never used Convoy I got a little bit confused how to use it in this more complex use case. As I have other demands from my employer if you could help me out on this or give a waiver in this method I would appreciate it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries! I'll see what I can do and push to this PR.

})
}