jarcoal / httpmock

HTTP mocking for Golang
http://godoc.org/github.com/jarcoal/httpmock
MIT License
1.93k stars 103 forks source link

Request do not propagate to package functions #17

Closed julio-vaz closed 5 years ago

julio-vaz commented 8 years ago

I'm trying to mock a URL that a function requests at the package I'm testing and it still executes the request to the original URL instead of the mocked one that we defined. Is there support for acessing mocked urls at package functions or only inside the test?

mockedResponse := map[string]interface{}{
        "data": []map[string]interface{}{
            map[string]interface{}{
                "key": "pipitchu",
            },
        },
        "message": nil,
        "success": true,
    }

        url := "https://test.pipitchu.com/123"
    httpmock.RegisterResponder("GET", url,
        func(req *http.Request) (*http.Response, error) {
            resp, err := httpmock.NewJsonResponse(200, mockedResponse)
            if err != nil {
                return httpmock.NewStringResponse(500, ""), nil
            }
            return resp, nil
        },
    )

key, err := GetKey(123456)
// key is the "key" key of the first element of the data array
assert(t, "pipitchu", key)
// we're using testify

Thanks in advance!

tedeh commented 7 years ago

For anyone looking for a solution, it's called DisableTransportSwap and must be set like this on the gorequest package:

gorequest.DisableTransportSwap = true

Also see https://github.com/parnurzeal/gorequest/pull/59/files

JodeZer commented 7 years ago

@julio-vaz Hi,did u find a solution for this? @tedeh And l just use the simple http package in golang.

onetwopunch commented 7 years ago

I'm also having this issue. It works fin in 1.7.5 but as soon as we migrated to 1.8.1 all the tests that were dependent on HTTPMock broke. Is there any update from the maintainer @jarcoal on this?

dlebech commented 7 years ago

@onetwopunch Regarding your issue: That's weird. All the tests pass in 1.8 (added to Travis in #30), and it also works on a library I'm working on in 1.8. Here's a test that's working. Are you perhaps using other libraries that might swap out Go's default transport? This seems to have have been an issue with the gorequest package according to @tedeh's comment above.

Regarding the maintenance of this repo: @jarcoal is no longer maintaining the repo. He added myself and @SchumacherFM as maintainers a while ago.

SchumacherFM commented 7 years ago

Seems like people are relying on the global http.Client ... then swapping out transport in parallel situations and wondering why it breaks. In my own projects I never rely on the global http.Client and its Transport. I can also be wrong with my above assumptions ;-)

onetwopunch commented 7 years ago

@dlebech I'm not sure how this works in 1.8.1 given that http.Transport no longer supports RegisterResponder. This is likely my naive understanding of the library, but I ended up removing httpmock in favor of creating my own RoundTripper implementation and injecting the client, which I think is a much better design choice than overwriting a DefaultTransport globally for the scope of the test (someone else added this library since it works similarly to WebMock in Ruby). Anyway, thanks for the response 🙂

dlebech commented 7 years ago

@onetwopunch I'm not completely sure what you mean by "http.Transport no longer supports RegisterResponder" since this was never a native feature of Go?

Either way, httpmock actually does provide a way to use a non-default client but it is admittedly not well documented in the README. Instead of calling Activate() you can call ActivateNonDefault(client) with a client of your choice. Here's a test that shows this behavior. I'm not sure if this fits your use case, but just throwing it out there :)

maxatome commented 5 years ago

@julio-vaz ActivateNonDefault(client) should do the work. If you still encounter a problem, please comment or re-open.