httpspec

package
v0.29.1 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2021 License: Apache-2.0 Imports: 11 Imported by: 0

README

Table of Contents generated with DocToc

httpspec

httpspec allow you to create HTTP API specifications with ease.

Documentation

The documentation maintained in GoDoc, including the examples.

Usage

package mypkg

func TestMyHandlerCreate(t *testing.T) {
	s := testcase.NewSpec(t)

	// subject
	httpspec.SubjectLet(s, func(t *testcase.T) http.Handler {
		return MyHandler{}
	})

	// Arrange
	httpspec.ContentTypeIsJSON(s)
	httpspec.Method.LetValue(s, http.MethodPost)
	httpspec.Path.LetValue(s, `/`)
	httpspec.Body.Let(s, func(t *testcase.T) interface{} {
		// this will end up as {"foo":"bar"} in the request body
		return map[string]string{"foo": "bar"}
	})

	s.Then(`it will...`, func(t *testcase.T) {
		// Act
		rr := httpspec.SubjectGet(t)

		// Assert
		require.Equal(t, http.StatusOK, rr.Code)
		var resp CreateResponse
		require.Nil(t, json.Unmarshal(rr.Body.Bytes(), &resp))
		// ...
	})
}

Documentation

Overview

Example (Usage)
var tb testing.TB
s := testcase.NewSpec(tb)

// subject
httpspec.SubjectLet(s, func(t *testcase.T) http.Handler {
	return MyHandler{}
})

// Arrange
httpspec.ContentTypeIsJSON(s)
httpspec.Method.LetValue(s, http.MethodPost)
httpspec.Path.LetValue(s, `/`)
httpspec.Body.Let(s, func(t *testcase.T) interface{} {
	// this will end up as {"foo":"bar"} in the request body
	return map[string]string{"foo": "bar"}
})

s.Then(`it will...`, func(t *testcase.T) {
	// Act
	rr := httpspec.SubjectGet(t)

	// Assert
	require.Equal(t, http.StatusOK, rr.Code)
	var resp CreateResponse
	require.Nil(t, json.Unmarshal(rr.Body.Bytes(), &resp))
	// ...
})
Output:

Example (UsageWithDotImport)
s := testcase.NewSpec(testingT)

SubjectLet(s, func(t *testcase.T) http.Handler { return MyHandler{} })

s.Before(func(t *testcase.T) {
	t.Log(`given authentication header is set`)
	HeaderGet(t).Set(`X-Auth-Token`, `token`)
})

s.Describe(`GET / - list of X`, func(s *testcase.Spec) {
	Method.LetValue(s, http.MethodGet)
	Path.LetValue(s, `/`)

	var onSuccess = func(t *testcase.T) ListResponse {
		rr := SubjectGet(t)
		require.Equal(t, http.StatusOK, rr.Code)
		// unmarshal the response from rr.body
		return ListResponse{}
	}

	s.And(`something is set in the query`, func(s *testcase.Spec) {
		s.Before(func(t *testcase.T) {
			QueryGet(t).Set(`something`, `value`)
		})

		s.Then(`it will react to it as`, func(t *testcase.T) {
			listResponse := onSuccess(t)
			// assert
			_ = listResponse
		})
	})

	s.Then(`it will return the list of resource`, func(t *testcase.T) {
		listResponse := onSuccess(t)
		// assert
		_ = listResponse
	})
})

s.Describe(`GET /{resourceID} - show X`, func(s *testcase.Spec) {
	Method.LetValue(s, http.MethodGet)
	Path.Let(s, func(t *testcase.T) interface{} {
		return fmt.Sprintf(`/%s`, t.I(`resourceID`))
	})

	var onSuccess = func(t *testcase.T) ShowResponse {
		rr := SubjectGet(t)
		require.Equal(t, http.StatusOK, rr.Code)
		// unmarshal the response from rr.body
		return ShowResponse{}
	}

	s.Then(`it will return the resource 'show'' representation`, func(t *testcase.T) {
		showResponse := onSuccess(t)
		// assert
		_ = showResponse
	})
})
Output:

Index

Examples

Constants

View Source
const (
	ContextVarName = letVarPrefix + `context`
)

Variables

View Source
var (
	Handler = testcase.Var{Name: `httpspec:Handler`}
	Context = testcase.Var{Name: `httpspec:Context`, Init: func(t *testcase.T) interface{} {
		return context.Background()
	}}
	Method = testcase.Var{Name: `httpspec:Method`, Init: func(t *testcase.T) interface{} {
		return http.MethodGet
	}}
	Path = testcase.Var{Name: `httpspec:Path`, Init: func(t *testcase.T) interface{} {
		return `/`
	}}
	Body = testcase.Var{Name: `httpspec:Body`, Init: func(t *testcase.T) interface{} {
		return &bytes.Buffer{}
	}}
	Query = testcase.Var{Name: `httpspec:QueryGet`, Init: func(t *testcase.T) interface{} {
		return url.Values{}
	}}
	Header = testcase.Var{Name: `httpspec:HeaderGet`, Init: func(t *testcase.T) interface{} {
		return http.Header{}
	}}
)
View Source
var Subject = testcase.Var{
	Name: `httpspec:SubjectGet`,
	Init: func(t *testcase.T) interface{} { return Act(t) },
}

Functions

func Act added in v0.27.0

Act will make a request to the spec context it requires the following spec variables

  • MethodGet -> http MethodGet <string>
  • PathGet -> http PathGet <string>
  • query -> http query string <url.Values>
  • body -> http payload <io.Reader|io.ReadCloser>

func ContentTypeIsJSON added in v0.13.0

func ContentTypeIsJSON(s *testcase.Spec)
Example
s := testcase.NewSpec(testingT)

SubjectLet(s, func(t *testcase.T) http.Handler { return MyHandler{} })
ContentTypeIsJSON(s)

s.Describe(`POST / - create X`, func(s *testcase.Spec) {
	Method.LetValue(s, http.MethodPost)
	Path.LetValue(s, `/`)

	Body.Let(s, func(t *testcase.T) interface{} {
		// this will end up as {"foo":"bar"} in the request body
		return map[string]string{"foo": "bar"}
	})

	var onSuccess = func(t *testcase.T) CreateResponse {
		rr := SubjectGet(t)
		require.Equal(t, http.StatusOK, rr.Code)
		var resp CreateResponse
		require.Nil(t, json.Unmarshal(rr.Body.Bytes(), &resp))
		return resp
	}

	s.Then(`it will create a new resource`, func(t *testcase.T) {
		createResponse := onSuccess(t)
		// assert
		_ = createResponse
	})
})
Output:

func ContextGet added in v0.27.0

func ContextGet(t *testcase.T) context.Context

ContextGet allow to retrieve the current test scope's request context.

func Debug added in v0.5.1

func Debug(s *testcase.Spec)

func HeaderGet added in v0.27.0

func HeaderGet(t *testcase.T) http.Header

HeaderGet allows you to set the current test scope's http PathGet for SubjectGet.

func QueryGet added in v0.27.0

func QueryGet(t *testcase.T) url.Values

QueryGet allows you to retrieve the current test scope's http PathGet query that will be used for SubjectGet. In a Before Block you can access the query and then specify the values in it.

func SubjectGet added in v0.27.0

func SubjectGet(t *testcase.T) *httptest.ResponseRecorder

func SubjectLet added in v0.27.0

func SubjectLet(s *testcase.Spec, subject func(t *testcase.T) http.Handler)

SubjectLet prepares the current testcase spec scope to be ready for http handler testing.

You define your spec subject with this and all the request will be pointed towards this.

Types

This section is empty.

Jump to

Keyboard shortcuts

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