h2non / gock

HTTP traffic mocking and testing made easy in Go ༼ʘ̚ل͜ʘ̚༽
https://pkg.go.dev/github.com/h2non/gock
MIT License
2.09k stars 107 forks source link

Only mock a certain URL #39

Closed bogdanpetrea closed 5 years ago

bogdanpetrea commented 5 years ago

Hi, It's not obvious to me how I can mock only the URLs I want and let the other requests pass.

requestMock := gock.New("test.com")
// Return empty list of tokens
requestMock.Get("/api/v1/users").
    Persist().
    Reply(200).
    JSON(map[string][]string{})

When running the tests I get:

Expected error:
          <*url.Error | 0xc000a2d440>: {
              Op: "Get",
              URL: "http://127.0.0.1:34161/api?timeout=32s",
              Err: {
                  s: "gock: cannot match any request",
              },
          }
          Get http://127.0.0.1:34161/api?timeout=32s: gock: cannot match any request
      not to have occurred

I don't want to mock http://127.0.0.1:34161/api?timeout=32s.

I have tried .EnableNetworking(), but I don't get the mocked response. I get something like this instead:

{"error": "Get test.com/api/v1/users: unsupported protocol scheme \"\""}
h2non commented 5 years ago

You must add the protocol scheme:

requestMock := gock.New("http://test.com")
// Return empty list of tokens
requestMock.Get("/api/v1/users").
    Persist().
    Reply(200).
    JSON(map[string][]string{})

I don't want to mock http://127.0.0.1:34161/api?timeout=32s. I have tried .EnableNetworking(), but I don't get the mocked response. I get something like this instead:

All you need here is enabling the networking mode as you mention.

bogdanpetrea commented 5 years ago

I didn't mention but I tried both without and with protocol (http and https). EDIT: Oh, I'm not doing the request right... EDIT2: Yup, that was it. I had to specify the protocol there. Thanks.

eloo commented 4 years ago

Hi, i would like to reopen this issue because it seems that its still not working as expected..

I'm trying mostly the same. Mocking just a single call. But i'm not able to get it working.

I've prepared a minimal working example what i'm doing

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"

    "gopkg.in/h2non/gock.v1"
)

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

    res, err := http.Get("http://server.com/bar")

    fmt.Println("Response Info:")
    fmt.Println("Error      :", err)
    body, _ := ioutil.ReadAll(res.Body)
    fmt.Println("Status Code:", res.StatusCode)
    fmt.Println("Body       :\n", string(body))
    fmt.Println()

    res, err = http.Get("https://httpbin.org/get")

    fmt.Println("Response Info:")
    fmt.Println("Error      :", err)
    body, _ = ioutil.ReadAll(res.Body)
    fmt.Println("Status Code:", res.StatusCode)
    fmt.Println("Body       :\n", string(body))
    fmt.Println()
}

As you can see i'm trying to mock the call against "server.com" and want to enableNetworking for everything else. But with the example above the mock for server.com is not working.. there is still networking enabled for this call. If a remove the enableNetworking above i get the mock working but the call to httpbin is not working..

Maybe i have missed something but i cant find the issue.

Thanks for your help eloo