h2non / gock

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

can not mock successfully when using `gock.EnableNetworking()` #93

Open vox-vox-vox opened 2 years ago

vox-vox-vox commented 2 years ago

In my project , I have to use gock.EnableNetworking() so that the real HTTP request will be requested if the mock can not be matched.
However, in this case, the url in gock.New(...) has to be a real url. If I use a random url such as "http://123456/bar" , an error will happened :
GET http://123456/bar Get "http://123456/bar": dial tcp 0.1.226.64:80: connect: no route to host

the whole code is as follow


package main

import (
        "testing"
        "github.com/ahuigo/requests"
        "gopkg.in/h2non/gock.v1"
)

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

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

        // send request
        resp, err := requests.Get("http://123456/bar")
        if err != nil {
                t.Fatal(err)
        }
        t.Log(resp.R.StatusCode)
        t.Log(resp.Text())
}``` 

In my view, the mock url should not be constrained to be accessible  when the pattern is matched . 
thanks~ 
h2non commented 2 years ago

You should not enable the real networking mode if the requests you are testing are not valid network reachable hosts unless you want to explicitly test that scenario. Simply ignore the EnableNetworking() call.

vox-vox-vox commented 2 years ago

now I have to test a func,consists of 4 http call,let‘s call them A,B,C,D.

A and D are real http call, B and C are the requests that I want to mock. I don't want the mock in B,C to influence the real http request in A,D, and I can not separate them since I need to test the whole func

what should I do in this condition with out EnableNetworking()

thanks~

h2non commented 2 years ago

You can enable the real networking mode per specific mock instance instead of enabling it globally: https://github.com/h2non/gock/blob/master/request.go#L294

RychEmrycho commented 9 months ago

hi @h2non, in the example you provided, i saw this defer statement.

func main() {
    defer gock.Disable()
    defer gock.DisableNetworking()

    srv := startHTTPServer()
    defer srv.Close()

    // Register our local server
    gock.New(srv.URL).
        EnableNetworking()

and i have two questions:

  1. supposed my SIT looks like this, where should i put the gock.Disable()? should i add it for every test case or only on TearDownSuite? im worried if i put it on every test cases, it will interfere with other test case not that im sure if its possible tho.
    
    type MySuite {
    suite.Suite
    }

func TestMySuite(t *testing.T) {suite.Run(t, new(MySuite)}

func (s *MySuite) SetupSuite {}

func (s *MySuite) TearDownSuite {}

func (s *MySuite) TestCaseOne {}

func (s *MySuite) TestCaseTwo {}

func (s *MySuite) TestCaseThree {}


2. why do we need call `defer gock.DisableNetworking()` that given that we only enable networking for the particular request only, unless we enable global `gock.EnableNetworking()`, then it makes sense for me. is there any reason for this?

Thanks.