levigross / grequests

A Go "clone" of the great and famous Requests library
Apache License 2.0
2.14k stars 138 forks source link

how can I mock http service with h2non/gock and levigross/grequests #60

Closed harobed closed 6 years ago

harobed commented 6 years ago

Hi,

how can I mock http service with h2non/gock and levigross/grequests?

Best regards, Stéphane

levigross commented 6 years ago

I will look into it -- at the moment I don't support mocking the library

marcosinger commented 6 years ago

@harobed At least for me it works like charm. Below I implemented the simple mocking available at gock readme just changing the http call:

package test

import (
    "testing"

    "github.com/h2non/gock"
    "github.com/levigross/grequests"
    "github.com/nbio/st"
)

func TestSimple(t *testing.T) {
    defer gock.Off()

    gock.New("http://foo.com").
        Get("/bar").
        Reply(200).
        JSON(map[string]string{"foo": "bar"})

    res, err := grequests.Get("http://foo.com/bar", nil)
    st.Expect(t, err, nil)
    st.Expect(t, res.StatusCode, 200)

    body := res.Bytes()
    st.Expect(t, string(body)[:13], `{"foo":"bar"}`)

    // Verify that we don't have pending mocks
    st.Expect(t, gock.IsDone(), true)
}

I don't know if it helps you :)

levigross commented 6 years ago

Thanks :)