mock

package module
v2.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 10, 2017 License: MIT Imports: 3 Imported by: 3

README

gentleman-mock Build Status GoDoc Coverage Status Go Report Card

gentleman's plugin for simple HTTP mocking via gock.

Installation

go get -u gopkg.in/h2non/gentleman-mock.v2

Versions

  • v1 - First version, uses gentleman@v1 and gock@v1.
  • v2 - Latest version, uses gentleman@v2 and gock@v1.

API

See godoc reference for detailed API documentation.

Example

package main

import (
  "fmt"

  "gopkg.in/h2non/gentleman.v2"
  "gopkg.in/h2non/gentleman-mock.v2"
)

func main() {
  defer mock.Disable()

  // Configure the mock via gock
  mock.New("http://httpbin.org").Get("/*").Reply(204).SetHeader("Server", "gock")

  // Create a new client
  cli := gentleman.New()

  // Register the mock plugin at client level
  cli.Use(mock.Plugin)

  // Create a new request based on the current client
  req := cli.Request()

  // Define base URL
  req.URL("http://httpbin.org")

  // Define the URL path at request level
  req.Path("/status/503")

  // Set a new header field
  req.SetHeader("Client", "gentleman")

  // Perform the request
  res, err := req.Send()
  if err != nil {
    fmt.Printf("Request error: %s\n", err)
    return
  }
  if !res.Ok {
    fmt.Printf("Invalid server response: %d\n", res.StatusCode)
    return
  }

  fmt.Printf("Status: %d\n", res.StatusCode)
  fmt.Printf("Header: %s\n", res.Header.Get("Server"))
}

License

MIT - Tomas Aparicio

Documentation

Index

Constants

View Source
const Version = "1.0.0"

Version defines the package semantic version

Variables

View Source
var Plugin = plugin.NewPhasePlugin("before dial", func(ctx *context.Context, h context.Handler) {
	gock.InterceptClient(ctx.Client)
	h.Next(ctx)
})

Plugin exports the mock plugin

Functions

func Disable

func Disable()

Disable disables the registered mocks. It's a shorthand to gock.Disable().

func New

func New(uri string) *gock.Request

New creates a new gock mock. It's a shorthand to gock.New().

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL