resttest

package
v0.2.64 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2024 License: MIT Imports: 1 Imported by: 1

Documentation

Overview

Package resttest provides utilities to test REST API.

Deprecated: please use github.com/bool64/httpmock.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client deprecated

type Client = httpmock.Client

Client keeps state of expectations.

Deprecated: please use httpmock.Client.

func NewClient deprecated

func NewClient(baseURL string) *Client

NewClient creates client instance, baseURL may be empty if Client.SetBaseURL is used later.

Deprecated: please use httpmock.NewClient.

Example
package main

import (
	"fmt"
	"net/http"

	"github.com/bool64/httpmock"
)

func main() {
	// Prepare server mock.
	sm, url := httpmock.NewServer()
	defer sm.Close()

	// This example shows Client and ServerMock working together for sake of portability.
	// In real-world scenarios Client would complement real server or ServerMock would complement real HTTP client.

	// Set successful expectation for first request out of concurrent batch.
	exp := httpmock.Expectation{
		Method:     http.MethodPost,
		RequestURI: "/foo?q=1",
		RequestHeader: map[string]string{
			"X-Custom":     "def",
			"X-Header":     "abc",
			"Content-Type": "application/json",
		},
		RequestBody:  []byte(`{"foo":"bar"}`),
		Status:       http.StatusAccepted,
		ResponseBody: []byte(`{"bar":"foo"}`),
	}
	sm.Expect(exp)

	// Set failing expectation for other requests of concurrent batch.
	exp.Status = http.StatusConflict
	exp.ResponseBody = []byte(`{"error":"conflict"}`)
	exp.Unlimited = true
	sm.Expect(exp)

	// Prepare client request.
	c := httpmock.NewClient(url)
	c.ConcurrencyLevel = 50
	c.Headers = map[string]string{
		"X-Header": "abc",
	}

	c.Reset().
		WithMethod(http.MethodPost).
		WithHeader("X-Custom", "def").
		WithContentType("application/json").
		WithBody([]byte(`{"foo":"bar"}`)).
		WithURI("/foo?q=1").
		Concurrently()

	// Check expectations errors.
	fmt.Println(
		c.ExpectResponseStatus(http.StatusAccepted),
		c.ExpectResponseBody([]byte(`{"bar":"foo"}`)),
		c.ExpectOtherResponsesStatus(http.StatusConflict),
		c.ExpectOtherResponsesBody([]byte(`{"error":"conflict"}`)),
	)

}
Output:

<nil> <nil> <nil> <nil>

type Expectation deprecated added in v0.1.5

type Expectation = httpmock.Expectation

Expectation describes expected request and defines response.

Deprecated: please use httpmock.Expectation.

type ServerMock added in v0.1.5

type ServerMock = httpmock.Server

ServerMock serves predefined response for predefined request.

func NewServerMock deprecated added in v0.1.5

func NewServerMock() (*ServerMock, string)

NewServerMock creates mocked server.

Deprecated: please use httpmock.NewServer.

Jump to

Keyboard shortcuts

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