Giter Site home page Giter Site logo

Comments (7)

arcln avatar arcln commented on May 21, 2024 2

I join others thinking it should be possible to test RoundTrippers.

A workaround is to use gock.DefaultTransport as the underlying transport in your custom transport :

// Instead of intercepting the http client
// gock.InterceptClient(httpClient)

// Send requests to gock's transport manually
myTransport := httpClient.Transport.(*MyTransport)
myTransport.delegateTransport = gock.DefaultTransport

from gock.

h2non avatar h2non commented on May 21, 2024

gock does a runtime patching in order to intercept HTTP traffic at http.Transport > RoundTrip level, hence that's the reason it doesn't work if you are overwriting the transport RoundTrip with a custom implementation after mocking the http.Client.
You can see implementation details here: https://github.com/h2non/gock/blob/master/transport.go#L25-L86

That being said, it's not recommended that your custom logic goes at transport level. That responsability should be done in a higher layer, not during the request round trip phase.

If you need to write non-trivial HTTP clients that share intermediate logic, I would suggest you taking a look to gentleman toolkit, which allows you to define plugabble middleware logic easily: https://github.com/h2non/gentleman

from gock.

billinghamj avatar billinghamj commented on May 21, 2024

+1 to this. We use a custom transport to apply auth. It is entirely recommended to set your transport-layer auth using a custom transport.

At the very least, it should detect custom transports and warn the user, and it should be documented as a limitation in the readme.

Or, yanno, it could just wrap the custom transport.

from gock.

billinghamj avatar billinghamj commented on May 21, 2024

If you want to use TLS client certificates, then you HAVE to use a custom transport

from gock.

leandromoreira avatar leandromoreira commented on May 21, 2024

It seems also that the way to add keepalive at client side is by swaping to a custom transport.

from gock.

martin-dos-santos avatar martin-dos-santos commented on May 21, 2024

+1

from gock.

wildsurfer avatar wildsurfer commented on May 21, 2024

Facing the same issue with testing the custom roundtripper

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.