Search code examples
httpgotestinghttp2

Testing http.Pusher and the push function in golang


I am trying to write a unit test for http.Pusher. I tried using httptest.NewRecorder() as http.ResponseWriter but the type conversion failed. How else can I write the test?

    //My function 
    func push(w http.ResponseWriter, resource string) error {
        pusher, ok := w.(http.Pusher)
        if ok {
            return pusher.Push(resource, nil)
        }
        return fmt.Errorf("Pusher not supported")
    }

    //My test 
    func TestPush(t *testing.T) {
        resource := "static/css/main.css"
        response := httptest.NewRecorder()
        got := push(response, resource)

        if got != nil {
            t.Errorf("Error : %v", got)
        }
    }

The output is "Pusher not supported", which I assume that w.(http.Pusher) failed.


Solution

  • You may create a mocked http.ResponseWriter that also implements http.Pusher, and pass that during testing.

    Here's a simple implementation that suits your testable function:

    type pusher struct {
        http.ResponseWriter
        err    error // err to return from Push()
        target string
        opts   *http.PushOptions
    }
    
    func (p *pusher) Push(target string, opts *http.PushOptions) error {
        // record passed arguments for later inspection
        p.target = target
        p.opts = opts
        return p.err
    }
    

    Example test function:

    func TestPush(t *testing.T) {
        resource := "static/css/main.css"
        p := &pusher{}
        err := push(p, resource)
    
        if err != p.err {
            t.Errorf("Expected: %v, got: %v", p.err, err)
        }
        if resource != p.target {
            t.Errorf("Expected: %v, got: %v", p.target, resource)
        }
    }
    

    Note that this simple pusher embeds the http.ResponseWriter type which will make it itself an http.ResponseWriter (it will make pusher implement http.ResponseWriter). During the test we left this field nil because the testable push() function did not use anything from it. If your real function would call methods of it such as ResponseWriter.Header(), that would cause a runtime panic of course. In that case you have to provide a valid http.ResponseWriter too, e.g.:

    p := &pusher{ResponseWriter: httptest.NewRecorder()}