FAQ
As recommended, I share my http.Clients throughout my program. As I'm
setting up unit testing, I'm realizing that there are a number of places
where I'm making external API requests that don't necessarily need to be
tested. Is there a way to overwrite my client in testing with a custom
transport or roundtripper that restricts the fetches to only specific
allowed urls? I only want to allow localhost + connections to httptest
servers that I spin up for mocks.

To be clear, I know how to set up my tests so I can overwrite the
destination url or pass in interfaces. This is more of a failsafe for
mistakes or jr devs, to keep from accidentally creating thousands of
unnecessary clients and campaigns if someone forgets to set things up
properly. :)

Thanks,
Derek

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Tamás Gulácsi at May 15, 2015 at 8:13 pm
    You want a net-jail, and you can achieve such thing by deleting the default gateway or using a spec filtering gateway.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
    For more options, visit https://groups.google.com/d/optout.
  • Lars Seipel at May 15, 2015 at 8:30 pm

    On Fri, May 15, 2015 at 10:38:29AM -0700, Derek Perkins wrote:
    tested. Is there a way to overwrite my client in testing with a custom
    transport or roundtripper that restricts the fetches to only specific
    allowed urls? I only want to allow localhost + connections to httptest
    servers that I spin up for mocks.
    You could set a custom dial function on the client's transport. Check
    the addr and only forward to net.Dial if it's localhost or whatever you
    want to allow. See the docs for http.Transport, note the Dial field.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMay 15, '15 at 6:37p
activeMay 15, '15 at 8:30p
posts3
users3
websitegolang.org

People

Translate

site design / logo © 2023 Grokbase