Code Monkey home page Code Monkey logo

Comments (6)

lneves12 avatar lneves12 commented on May 8, 2024 2

Finally found a solution:

type timeoutError struct {
	err     string
	timeout bool
}

func (e *timeoutError) Error() string {
	return e.err
}
func (e *timeoutError) Timeout() bool {
	return e.timeout
}

func (e *timeoutError) Temporary() bool {
	return true
}

var _ = Describe("Testing", func() {
		It("client timeout", func() {
			defer gock.Off()

			gock.New("https://baseurl.com").
				Get("/url").
				ReplyError(&timeoutError{err: "net/http: timeout awaiting response headers", timeout: true})
		})
	})

})

I will open a PR later adding this to the examples list

from gock.

h2non avatar h2non commented on May 8, 2024

You can use the Delay(duration) method:
https://godoc.org/github.com/h2non/gock#Response.Delay

Alternatively, you can simulate the native timeout error via:
https://godoc.org/github.com/h2non/gock#Response.SetError

from gock.

lneves12 avatar lneves12 commented on May 8, 2024

I tried the Dealy(duration) method, but I couldn't simulate the http.client timeout:

var httpClient = &http.Client{Timeout: 1 * time.Second}

I had to use the solution on my second comment. Not sure if it's the best solution though

from gock.

h2non avatar h2non commented on May 8, 2024

It depends on what type of error you want to simulate.
If you simply want to test the error handling for a timeout-like situation, that's fine.

from gock.

lneves12 avatar lneves12 commented on May 8, 2024

yeah, I want to simulate an offline situation, where http.get will timeout. Do you think it's worth to add this as an example to docs? or should we just close the issue?

from gock.

h2non avatar h2non commented on May 8, 2024

Adding an example file here should be fine. Feel free to send a PR!

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.