Coder Social home page Coder Social logo

Only mock a certain URL about gock HOT 3 CLOSED

h2non avatar h2non commented on May 21, 2024
Only mock a certain URL

from gock.

Comments (3)

h2non avatar h2non commented on May 21, 2024

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.

from gock.

bogdanpetrea avatar bogdanpetrea commented on May 21, 2024

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.

from gock.

eloo avatar eloo commented on May 21, 2024

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

from gock.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.