jeudi 20 juin 2019

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.

Aucun commentaire:

Enregistrer un commentaire