Convert Figma logo to code with AI

h2non logogock

HTTP traffic mocking and testing made easy in Go ༼ʘ̚ل͜ʘ̚༽

2,079
107
2,079
45

Top Related Projects

HTTP mocking for Golang

22,996

A toolkit with common assertions and mocks that plays nicely with the standard library

5,959

A mock code autogenerator for Go

Quick Overview

gock is a HTTP traffic mocking and testing library for Go. It provides a simple and intuitive API to define, configure, and control HTTP mocks, allowing developers to write robust and maintainable tests for their Go applications.

Pros

  • Flexible Mocking: gock allows you to define complex HTTP mocking scenarios, including request matching, response configuration, and dynamic response generation.
  • Intuitive API: The library offers a clean and easy-to-use API, making it straightforward to set up and manage mocks in your test suites.
  • Supports Multiple Protocols: gock supports mocking HTTP, HTTPS, and even WebSocket connections, providing a comprehensive solution for testing various types of network interactions.
  • Extensible: The library is designed to be extensible, allowing developers to create custom matchers and responders to fit their specific testing needs.

Cons

  • Limited Ecosystem: Compared to some other mocking libraries in the Go ecosystem, gock may have a smaller community and fewer third-party integrations.
  • Potential Performance Impact: Depending on the complexity of your mocking scenarios, the library may introduce some overhead in your test suite, which could impact overall test performance.
  • Lack of Automatic Verification: gock does not automatically verify that all expected requests were made during a test, requiring developers to manually check the mock's interaction history.
  • Potential Maintenance Overhead: As the project evolves, developers may need to update their tests to keep up with any changes in the library's API or behavior.

Code Examples

Defining a Simple Mock

import (
    "github.com/h2non/gock"
    "net/http"
)

func ExampleSimpleMock() {
    defer gock.Off()

    gock.New("http://api.example.com")
        .Get("/users/1")
        .Reply(200)
        .JSON(map[string]string{"name": "John Doe"})

    resp, err := http.Get("http://api.example.com/users/1")
    if err != nil {
        // Handle error
    }
    defer resp.Body.Close()

    // Verify the response
    // ...
}

Matching Requests with Conditions

import (
    "github.com/h2non/gock"
    "net/http"
)

func ExampleRequestMatching() {
    defer gock.Off()

    gock.New("http://api.example.com")
        .Post("/users")
        .MatchType("json")
        .JSON(map[string]string{"name": "John Doe"})
        .Reply(201)
        .JSON(map[string]string{"id": "1234"})

    resp, err := http.Post("http://api.example.com/users", "application/json", bytes.NewBufferString(`{"name":"John Doe"}`))
    if err != nil {
        // Handle error
    }
    defer resp.Body.Close()

    // Verify the response
    // ...
}

Mocking Dynamic Responses

import (
    "github.com/h2non/gock"
    "net/http"
)

func ExampleDynamicResponse() {
    defer gock.Off()

    gock.New("http://api.example.com")
        .Get("/users/(?P<id>\\d+)")
        .SetMatcher(gock.NewMatcher().
            WithPathPattern(`^/users/(\d+)$`).
            WithQueryString("name=John"))
        .Reply(200)
        .JSON(func(req *http.Request, _ *gock.Response) (interface{}, error) {
            id := gock.MatcherInfo(req).Get("id")
            return map[string]string{"id": id, "name": "John Doe"}, nil
        })

    resp, err := http.Get("http://api.example.com/users/1234?name=John")
    if err != nil {
        // Handle error
    }
    defer resp.Body.Close()

    // Verify the response
    // ...
}

Competitor Comparisons

HTTP mocking for Golang

Pros of jarcoal/httpmock

  • Supports a wider range of HTTP methods, including PATCH, OPTIONS, and DELETE.
  • Provides a more intuitive and flexible API for defining and managing HTTP mocks.
  • Includes support for query parameters and request headers in mock definitions.

Cons of jarcoal/httpmock

  • Slightly less mature and actively maintained compared to h2non/gock.
  • May have fewer third-party integrations and community support.
  • Potentially less comprehensive documentation and examples.

Code Comparison

h2non/gock

gock.New("http://example.com").
    Get("/api/users").
    Reply(200).
    JSON(map[string]string{"name": "John Doe"})

jarcoal/httpmock

httpmock.RegisterResponder("GET", "http://example.com/api/users",
    httpmock.NewStringResponder(200, `{"name": "John Doe"}`))
22,996

A toolkit with common assertions and mocks that plays nicely with the standard library

Pros of Testify

  • Testify provides a wide range of assertion functions, making it easier to write expressive and readable tests.
  • The library includes support for mocking, which can be useful for isolating dependencies in tests.
  • Testify is well-documented and has a large community, making it easier to find examples and get support.

Cons of Testify

  • Testify may be overkill for simple projects that don't require a lot of advanced testing features.
  • The library can add some overhead to your project, as it requires importing and using the Testify package.
  • Testify may not be as flexible or customizable as some other testing frameworks, depending on your specific needs.

Code Comparison

Testify:

func TestMyFunction(t *testing.T) {
    assert.Equal(t, 42, myFunction())
    assert.NotNil(t, myOtherFunction())
}

Gock:

func TestMyHTTPCall(t *testing.T) {
    defer gock.Off()

    gock.New("https://example.com").
        Get("/api/data").
        Reply(200).
        JSON(map[string]interface{}{"data": "hello"})

    resp, err := http.Get("https://example.com/api/data")
    assert.NoError(t, err)
    assert.Equal(t, 200, resp.StatusCode)
}
5,959

A mock code autogenerator for Go

Pros of Mockery

  • Mockery provides a more comprehensive mocking solution, allowing you to mock not only HTTP requests but also other types of dependencies, such as database connections or external APIs.
  • Mockery has a more intuitive and user-friendly API, making it easier to set up and configure mocks for your tests.
  • Mockery supports a wider range of programming languages, including Go, Java, and Python, making it a more versatile tool.

Cons of Mockery

  • Mockery may have a steeper learning curve compared to Gock, as it offers more features and functionality.
  • Mockery may be overkill for simple HTTP mocking use cases, where Gock might be a more lightweight and focused solution.
  • Mockery's broader scope may make it more complex to set up and configure, especially for smaller projects.

Code Comparison

Gock (HTTP mocking):

gock.New("https://api.example.com")
    .Get("/users")
    .Reply(200)
    .JSON([]map[string]interface{}{
        {"id": 1, "name": "John Doe"},
        {"id": 2, "name": "Jane Doe"},
    })

Mockery (general mocking):

// Create a mock for an interface
mockService := new(MockService)

// Set expectations on the mock
mockService.On("GetUser", 1).Return(&User{ID: 1, Name: "John Doe"}, nil)
mockService.On("GetUser", 2).Return(&User{ID: 2, Name: "Jane Doe"}, nil)

// Use the mock in your test
user, err := mockService.GetUser(1)
assert.NoError(t, err)
assert.Equal(t, &User{ID: 1, Name: "John Doe"}, user)

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

gock GitHub release GoDoc Coverage Status Go Report Card license

Versatile HTTP mocking made easy in Go that works with any net/http based stdlib implementation.

Heavily inspired by nock. There is also its Python port, pook.

To get started, take a look to the examples.

Features

  • Simple, expressive, fluent API.
  • Semantic API DSL for declarative HTTP mock declarations.
  • Built-in helpers for easy JSON/XML mocking.
  • Supports persistent and volatile TTL-limited mocks.
  • Full regular expressions capable HTTP request mock matching.
  • Designed for both testing and runtime scenarios.
  • Match request by method, URL params, headers and bodies.
  • Extensible and pluggable HTTP matching rules.
  • Ability to switch between mock and real networking modes.
  • Ability to filter/map HTTP requests for accurate mock matching.
  • Supports map and filters to handle mocks easily.
  • Wide compatible HTTP interceptor using http.RoundTripper interface.
  • Works with any net/http compatible client, such as gentleman.
  • Network timeout/cancelation delay simulation.
  • Extensible and hackable API.
  • Dependency free.

Installation

go get -u github.com/h2non/gock

API

See godoc reference for detailed API documentation.

How it mocks

  1. Intercepts any HTTP outgoing request via http.DefaultTransport or custom http.Transport used by any http.Client.
  2. Matches outgoing HTTP requests against a pool of defined HTTP mock expectations in FIFO declaration order.
  3. If at least one mock matches, it will be used in order to compose the mock HTTP response.
  4. If no mock can be matched, it will resolve the request with an error, unless real networking mode is enable, in which case a real HTTP request will be performed.

Tips

Testing

Declare your mocks before you start declaring the concrete test logic:

func TestFoo(t *testing.T) {
  defer gock.Off() // Flush pending mocks after test execution

  gock.New("http://server.com").
    Get("/bar").
    Reply(200).
    JSON(map[string]string{"foo": "bar"})

  // Your test code starts here...
}

Race conditions

If you're running concurrent code, be aware that your mocks are declared first to avoid unexpected race conditions while configuring gock or intercepting custom HTTP clients.

gock is not fully thread-safe, but sensible parts are. Any help making gock more reliable in this sense is appreciated.

Define complex mocks first

If you're mocking a bunch of mocks in the same test suite, it's recommended to define the more concrete mocks first, and then the generic ones.

This approach usually avoids matching unexpected generic mocks (e.g: specific header, body payload...) instead of the generic ones that performs less complex matches.

Disable gock traffic interception once done

In other to minimize potential side effects within your test code, it's a good practice disabling gock once you are done with your HTTP testing logic.

A Go idiomatic approach for doing this can be using it in a defer statement, such as:

func TestGock (t *testing.T) {
	defer gock.Off()

	// ... my test code goes here
}

Intercept an http.Client just once

You don't need to intercept multiple times the same http.Client instance.

Just call gock.InterceptClient(client) once, typically at the beginning of your test scenarios.

Restore an http.Client after interception

NOTE: this is not required is you are using http.DefaultClient or http.DefaultTransport.

As a good testing pattern, you should call gock.RestoreClient(client) after running your test scenario, typically as after clean up hook.

You can also use a defer statement for doing it, as you do with gock.Off(), such as:

func TestGock (t *testing.T) {
	defer gock.Off()
	defer gock.RestoreClient(client)

	// ... my test code goes here
}

Examples

See examples directory for more featured use cases.

Simple mocking via tests

package test

import (
  "io/ioutil"
  "net/http"
  "testing"

  "github.com/nbio/st"
  "github.com/h2non/gock"
)

func TestSimple(t *testing.T) {
  defer gock.Off()

  gock.New("http://foo.com").
    Get("/bar").
    Reply(200).
    JSON(map[string]string{"foo": "bar"})

  res, err := http.Get("http://foo.com/bar")
  st.Expect(t, err, nil)
  st.Expect(t, res.StatusCode, 200)

  body, _ := ioutil.ReadAll(res.Body)
  st.Expect(t, string(body)[:13], `{"foo":"bar"}`)

  // Verify that we don't have pending mocks
  st.Expect(t, gock.IsDone(), true)
}

Request headers matching

package test

import (
  "io/ioutil"
  "net/http"
  "testing"

  "github.com/nbio/st"
  "github.com/h2non/gock"
)

func TestMatchHeaders(t *testing.T) {
  defer gock.Off()

  gock.New("http://foo.com").
    MatchHeader("Authorization", "^foo bar$").
    MatchHeader("API", "1.[0-9]+").
    HeaderPresent("Accept").
    Reply(200).
    BodyString("foo foo")

  req, err := http.NewRequest("GET", "http://foo.com", nil)
  req.Header.Set("Authorization", "foo bar")
  req.Header.Set("API", "1.0")
  req.Header.Set("Accept", "text/plain")

  res, err := (&http.Client{}).Do(req)
  st.Expect(t, err, nil)
  st.Expect(t, res.StatusCode, 200)
  body, _ := ioutil.ReadAll(res.Body)
  st.Expect(t, string(body), "foo foo")

  // Verify that we don't have pending mocks
  st.Expect(t, gock.IsDone(), true)
}

Request param matching

package test

import (
  "io/ioutil"
  "net/http"
  "testing"

  "github.com/nbio/st"
  "github.com/h2non/gock"
)

func TestMatchParams(t *testing.T) {
  defer gock.Off()

  gock.New("http://foo.com").
    MatchParam("page", "1").
    MatchParam("per_page", "10").
    Reply(200).
    BodyString("foo foo")

  req, err := http.NewRequest("GET", "http://foo.com?page=1&per_page=10", nil)

  res, err := (&http.Client{}).Do(req)
  st.Expect(t, err, nil)
  st.Expect(t, res.StatusCode, 200)
  body, _ := ioutil.ReadAll(res.Body)
  st.Expect(t, string(body), "foo foo")

  // Verify that we don't have pending mocks
  st.Expect(t, gock.IsDone(), true)
}

JSON body matching and response

package test

import (
  "bytes"
  "io/ioutil"
  "net/http"
  "testing"
	
	"github.com/nbio/st"
  "github.com/h2non/gock"
)

func TestMockSimple(t *testing.T) {
  defer gock.Off()

  gock.New("http://foo.com").
    Post("/bar").
    MatchType("json").
    JSON(map[string]string{"foo": "bar"}).
    Reply(201).
    JSON(map[string]string{"bar": "foo"})

  body := bytes.NewBuffer([]byte(`{"foo":"bar"}`))
  res, err := http.Post("http://foo.com/bar", "application/json", body)
  st.Expect(t, err, nil)
  st.Expect(t, res.StatusCode, 201)

  resBody, _ := ioutil.ReadAll(res.Body)
  st.Expect(t, string(resBody)[:13], `{"bar":"foo"}`)

  // Verify that we don't have pending mocks
  st.Expect(t, gock.IsDone(), true)
}

Mocking a custom http.Client and http.RoundTripper

package test

import (
  "io/ioutil"
  "net/http"
  "testing"

  "github.com/nbio/st"
  "github.com/h2non/gock"
)

func TestClient(t *testing.T) {
  defer gock.Off()

  gock.New("http://foo.com").
    Reply(200).
    BodyString("foo foo")

  req, err := http.NewRequest("GET", "http://foo.com", nil)
  client := &http.Client{Transport: &http.Transport{}}
  gock.InterceptClient(client)

  res, err := client.Do(req)
  st.Expect(t, err, nil)
  st.Expect(t, res.StatusCode, 200)
  body, _ := ioutil.ReadAll(res.Body)
  st.Expect(t, string(body), "foo foo")

  // Verify that we don't have pending mocks
  st.Expect(t, gock.IsDone(), true)
}

Enable real networking

package main

import (
  "fmt"
  "io/ioutil"
  "net/http"

  "github.com/h2non/gock"
)

func main() {
  defer gock.Off()
  defer gock.DisableNetworking()

  gock.EnableNetworking()
  gock.New("http://httpbin.org").
    Get("/get").
    Reply(201).
    SetHeader("Server", "gock")

  res, err := http.Get("http://httpbin.org/get")
  if err != nil {
    fmt.Errorf("Error: %s", err)
  }

  // The response status comes from the mock
  fmt.Printf("Status: %d\n", res.StatusCode)
  // The server header comes from mock as well
  fmt.Printf("Server header: %s\n", res.Header.Get("Server"))
  // Response body is the original
  body, _ := ioutil.ReadAll(res.Body)
  fmt.Printf("Body: %s", string(body))
}

Debug intercepted http requests

package main

import (
	"bytes"
	"net/http"
	
  "github.com/h2non/gock"
)

func main() {
	defer gock.Off()
	gock.Observe(gock.DumpRequest)

	gock.New("http://foo.com").
		Post("/bar").
		MatchType("json").
		JSON(map[string]string{"foo": "bar"}).
		Reply(200)

	body := bytes.NewBuffer([]byte(`{"foo":"bar"}`))
	http.Post("http://foo.com/bar", "application/json", body)
}

Hacking it!

You can easily hack gock defining custom matcher functions with own matching rules.

See add matcher functions and custom matching layer examples for further details.

License

MIT - Tomas Aparicio