Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
vox-vox-vox opened this issue Sep 2, 2021 · 4 comments
Open

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

vox-vox-vox opened this issue Sep 2, 2021 · 4 comments
Labels

Comments

@vox-vox-vox
Copy link

vox-vox-vox commented Sep 2, 2021

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~ 
@vox-vox-vox vox-vox-vox reopened this Sep 2, 2021
@vox-vox-vox vox-vox-vox changed the title can not can not mock successfully when using gock.EnableNetworking() Sep 2, 2021
@h2non
Copy link
Owner

h2non commented Sep 2, 2021

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
Copy link
Author

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
Copy link
Owner

h2non commented Sep 4, 2021

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

@h2non h2non added the question label Sep 4, 2021
@RychEmrycho
Copy link

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 {}
  1. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants