requesttest

package module
v0.0.0-...-372365b Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2019 License: MIT Imports: 3 Imported by: 0

README

requesttest

Go Report Card Godoc

Simple HTTP test library.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Echo

func Echo() (string, func())

Echo a request's method and payload in JSON.

Example
package main

import (
	"fmt"

	"github.com/hoanhan101/request"
	"github.com/hoanhan101/requesttest"
)

// Response describes a response from requesttest.Echo().
type Response struct {
	Method  string      `json:"method"`
	Payload interface{} `json:"payload"`
}

func main() {
	url, closer := requesttest.Echo()
	defer closer()

	r := new(Response)

	err := request.GetJSON(
		&request.Options{
			URL:     url,
			Payload: map[string]string{"k1": "v1"},
		},
		r,
	)
	if err != nil {
		fmt.Println(err)
	}

	fmt.Printf("%+v\n", r)
}
Output:

&{Method:GET Payload:k1=v1}

func Mock

func Mock(response interface{}) (string, func())

Mock a JSON response.

Example
package main

import (
	"fmt"

	"github.com/hoanhan101/request"
	"github.com/hoanhan101/requesttest"
)

// Response describes a response from requesttest.Echo().
type Response struct {
	Method  string      `json:"method"`
	Payload interface{} `json:"payload"`
}

func main() {
	url, closer := requesttest.Mock(`{"method":"GET","payload":{"k1":"k2"}}`)
	defer closer()

	r := new(Response)

	err := request.GetJSON(
		&request.Options{
			URL: url,
		},
		r,
	)
	if err != nil {
		fmt.Println(err)
	}

	fmt.Printf("%+v\n", r)
}
Output:

&{Method:GET Payload:map[k1:k2]}

Types

This section is empty.

Jump to

Keyboard shortcuts

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