testrequest

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2021 License: MIT Imports: 9 Imported by: 0

README

Image alt License Build Status Go Reference

Simple helpers for testing http-handlers and other handlers

Install

go get github.com/redagain/go-testrequest

Helpers

  • Test request builder
  • No-op http.ResponseWriter

Usage example

func TestHandler(t *testing.T) {
	type args struct {
		w http.ResponseWriter
		r *http.Request
	}
	tests := []struct {
		name    string
		args    args
		wantErr bool
	}{
		{
			name: "Success",
			args: args{
				w: testrequest.NopResponseWriter(),
				r: testrequest.Builder().SetJSONFromValue(
					map[string]interface{}{
						"title":   "The Go Programming Language",
						"isbn":    "978-0134190440",
						"authors": []string{"Alan A. A. Donovan", "Brian W. Kernighan"},
					}).
					SetAccept("application/json").
					SetBearerAuth("4mwbMA6zq9Nxf3XzLk9n01MJX57jdjMAdfYCaJu44vEUJVfdVLF9").
					Request(),
			},
			wantErr: false,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {    

Examples

  • simpleapi - examples of using helpers to test a simple API
  • clientcredentials - example of testing a function to extract client credentials from a http.Request

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NopResponseWriter

func NopResponseWriter() http.ResponseWriter

NopResponseWriter returns a http.ResponseWriter with a no-op Header, Write and WriteHeader methods. This is a simple stub for testing.

func SetDefaultTarget

func SetDefaultTarget(target string)

SetDefaultTarget sets default target for RequestBuilder

Types

type RequestBuilder

type RequestBuilder interface {
	// SetHeader sets the request's target.
	SetTarget(target string) RequestBuilder
	// SetMethod sets the request's HTTP method.
	SetMethod(method string) RequestBuilder
	// SetQuery sets the query for the request.
	SetQuery(q url.Values) RequestBuilder
	// SetQueryValue sets the query parameter for the request.
	SetQueryValue(key string, value ...string) RequestBuilder
	// SetHeader sets the header for the request.
	SetHeader(key string, value ...string) RequestBuilder
	// SetContentType sets the request's Content-Type header.
	//
	// Example of a value: application/json; charset=UTF-8.
	//
	// See RFC 7231, Section 3.1.1.5.
	SetContentType(value string) RequestBuilder
	// SetContentTypeWithParam sets the request's Content-Type header with parameter.
	//SetContentTypeWithParam(mimeType, param string) RequestBuilder
	// SetAccept sets the request's Accept header.
	SetAccept(value string) RequestBuilder
	// SetAcceptLanguage sets the request's Accept-Language header.
	SetAcceptLanguage(value string) RequestBuilder
	// SetCookies sets the request's cookie.
	SetCookies(cookie ...*http.Cookie) RequestBuilder
	// SetContext sets the request's context.
	SetContext(context context.Context) RequestBuilder
	// SetContextValue sets the request's context value.
	SetContextValue(key, value interface{}) RequestBuilder
	// SetBody sets the request's body.
	SetBody(reader io.Reader) RequestBuilder
	// SetPostForm sets the request's body as PostForm.
	// If PostForm is nil, it is initialized. The method is set as POST.
	// Content type as application/x-www-form-urlencoded.
	SetPostForm(postForm url.Values) RequestBuilder
	// SetPostFormValue sets field for the request's PostForm.
	// If PostForm is nil, it is initialized. The method is set as POST.
	// Content type is set as application/x-www-form-urlencoded.
	SetPostFormValue(key string, value ...string) RequestBuilder
	// SetJSON sets JSON-encoded data to the request body.
	//
	// If HTTP method is not set or GET or DELETE, the value is set as POST.
	//
	// If Content-Type header is not set, then the value is set as application/json;charset=UTF8.
	SetJSON(data []byte) RequestBuilder
	// SetJSONFromValue converts the value to JSON encoding
	// and sets data to the request body. An error encoding the value will cause a panic.
	//
	// If HTTP method is not set or GET or DELETE, the value is set as POST.
	//
	// If Content-Type header is not set, then the value is set as application/json;charset=UTF8.
	SetJSONFromValue(v interface{}) RequestBuilder
	// SetAuth sets the request's Authorization header.
	// Prefix specifies the authentication scheme.
	SetAuth(prefix, value string) RequestBuilder
	// SetBasicAuth sets Authorization header to use HTTP
	// Basic Authentication with the provided username and password.
	// See RFC 2617, Section 2.
	SetBasicAuth(username, password string) RequestBuilder
	// SetBearerAuth sets the request's Authorization header to use HTTP Bearer Authentication.
	// See RFC 6750, bearer tokens to access OAuth 2.0-protected resources.
	SetBearerAuth(token string) RequestBuilder
	// SetUserAgent sets User-Agent header.
	SetUserAgent(value string) RequestBuilder
	// Request constructs and returns a new incoming server http.Request for testing.
	//
	// If PostForm is initialized, the request body will be set as strings.Reader of its values.
	// Other method SetBody calls will be ignored.
	Request() *http.Request
}

A RequestBuilder interface defines methods for constructing a test http.Request.

func Builder

func Builder() RequestBuilder

Builder returns a new RequestBuilder for constructing a test http.Request. By default HTTP method is set as GET.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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