FAQ
I have code that makes an HTTP request to an external URL. Is there a way
to intercept the Go HTTP client to stub a response for a given URL? I want
to test the code without having to make a real HTTP request to an external
server.

Thanks!

-Scott

--
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 golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Naitik Shah at Aug 5, 2013 at 6:10 pm
    Either use net/http/httptest.Server or define your http.Client
    dependency as an interface where you can provide a mock in your tests.
    On Mon, Aug 5, 2013 at 7:52 AM, Scott Nelson wrote:
    I have code that makes an HTTP request to an external URL. Is there a way
    to intercept the Go HTTP client to stub a response for a given URL? I want
    to test the code without having to make a real HTTP request to an external
    server.

    Thanks!

    -Scott

    --
    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 golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.


    --
    -Naitik

    --
    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 golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Brad Fitzpatrick at Aug 5, 2013 at 6:26 pm
    Or modify the http.DefaultTransport global variable to be an in-memory
    fake/mock/whatever.


    On Mon, Aug 5, 2013 at 11:10 AM, Naitik Shah wrote:

    Either use net/http/httptest.Server or define your http.Client
    dependency as an interface where you can provide a mock in your tests.
    On Mon, Aug 5, 2013 at 7:52 AM, Scott Nelson wrote:
    I have code that makes an HTTP request to an external URL. Is there a way
    to intercept the Go HTTP client to stub a response for a given URL? I want
    to test the code without having to make a real HTTP request to an external
    server.

    Thanks!

    -Scott

    --
    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 golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.


    --
    -Naitik

    --
    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 golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    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 golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Scott Nelson at Aug 5, 2013 at 10:04 pm
    Thanks for the suggestions!
    On Monday, August 5, 2013 2:25:45 PM UTC-4, bradfitz wrote:

    Or modify the http.DefaultTransport global variable to be an in-memory
    fake/mock/whatever.



    On Mon, Aug 5, 2013 at 11:10 AM, Naitik Shah <n...@daaku.org <javascript:>
    wrote:
    Either use net/http/httptest.Server or define your http.Client
    dependency as an interface where you can provide a mock in your tests.

    On Mon, Aug 5, 2013 at 7:52 AM, Scott Nelson <sc...@scttnlsn.com<javascript:>>
    wrote:
    I have code that makes an HTTP request to an external URL. Is there a way
    to intercept the Go HTTP client to stub a response for a given URL? I want
    to test the code without having to make a real HTTP request to an external
    server.

    Thanks!

    -Scott

    --
    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 golang-nuts...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.


    --
    -Naitik

    --
    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 golang-nuts...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    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 golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Rémy Oudompheng at Aug 5, 2013 at 6:16 pm
    You can write a special HTTP proxy.

    Rémy.



    2013/8/5, Scott Nelson <scott@scttnlsn.com>:
    I have code that makes an HTTP request to an external URL. Is there a way
    to intercept the Go HTTP client to stub a response for a given URL? I want

    to test the code without having to make a real HTTP request to an external
    server.

    Thanks!

    -Scott

    --
    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 golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    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 golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Mathias Ringhof at Aug 6, 2013 at 5:09 am
    There is the httptest package:
    http://golang.org/pkg/net/http/httptest/

    I'm using the ResponseRecorder for unit testing, but there is also a Server for an end to end approach.

    --
    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 golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedAug 5, '13 at 2:52p
activeAug 6, '13 at 5:09a
posts6
users5
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase