hit

package module
v0.0.0-...-dd8d972 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2015 License: BSD-3-Clause Imports: 14 Imported by: 0

README

##hit A package for testing api endpoints.

Install:

go get github.com/mkopriva/hit

Example:

package api

import (
	"log"
	"net/http"
	"testing"

	"github.com/mkopriva/hit"
)

var hits = []hit.Hit{
	{"/welcome", hit.Requests{
		"GET": {{
			Header: hit.Header{"Authorization": {"345j9rhtg0394"}},
			Body:   nil,
			Want: hit.Response{
				Status: 200,
				Header: hit.Header{"Content-Type": {"application/json"}},
				Body:   hit.JSONBody{"message": "Hello World"},
			},
		}},
	}},
	{"/user", hit.Requests{
		"POST": {{
			Header: nil,
			Body:   hit.FormBody{"email": {"foo@example.com"}},
			Want: hit.Response{
				Status: 201,
				Header: hit.Header{"Location": {"http://example.com/users/123"}},
				Body:   nil,
			},
		}},
		"PATCH": {{
			Header: hit.Header{"Authorization": {"345j9rhtg0394"}},
			Body:   hit.JSONBody{"email": "bar@example.com"},
			Want:   hit.Response{204, nil, nil},
		}, {
			Header: nil,
			Body:   hit.JSONBody{"email": "bar@example.com"},
			Want:   hit.Response{401, nil, nil},
		}},
	}},
}

func TestAPI(t *testing.T) {

	// setup your api handlers here...

	go func() {
		log.Fatal(http.ListenAndServe(hit.Addr, nil))
	}()

	for _, h := range hits {
		h.Test(t)
	}
}

Skipping Requests Example:


var h := hit.Hit{
	"/signin", hit.Requests{
		"POST": {{
			// skipping an individual Request
			Skip: true,
			Body: hit.FormBody{"email":{"jdoe@example.com"}, "pass":{"wrongpass"}}
			Want: hit.Response{400, nil, nil},
		}, {
			Body: hit.FormBody{"email":{"jdoe@example.com"}, "pass":{"correctpass"}}
			Want: hit.Response{ 302, hit.Header{"Location": {"http://example.com/account"}}, nil},
		}},
		// ...

	// skipping all of Hit's Requests
	}.Skip(),
}

Documentation

Overview

Copyright (c) 2015, Marian Kopriva All rights reserved. Licensed under BSD, see LICENSE for details.

Index

Constants

View Source
const (
	// ANSI color values used to colorize terminal output for better readability.
	RedColor    = "\033[91m"
	YellowColor = "\033[93m"
	PurpleColor = "\033[95m"
	CyanColor   = "\033[96m"
	StopColor   = "\033[0m"
)

Variables

View Source
var (
	// Addr is the TCP network address used to construct requests. The user
	// is free to set it to any other address value they want to test.
	Addr = "localhost:3456"
)

Functions

This section is empty.

Types

type Bodyer

type Bodyer interface {
	Type() string
	Body() (io.Reader, error)
}

Bodyer

type File

type File struct {
	Type     string
	Name     string
	Contents string
}

The type File should be used in combination with the type MultipartBody to represent a file being uploaded in an http request.

type FormBody

type FormBody map[string][]string

FormBody represents an http request body whose content is of type application/x-www-form-urlencoded.

func (FormBody) Body

func (b FormBody) Body() (io.Reader, error)

Body implements the Bodyer interface by serializing the receiver's contents into a url encoded string and returning it as an io.Reader.

func (FormBody) Type

func (FormBody) Type() string

Type returns the FormBody's media type.

type Header http.Header

Header represents an HTTP Header.

func (Header) AddTo

func (h Header) AddTo(r *http.Request)

AddTo sets all of the receiver's values to the specified http.Request's header.

func (Header) Compare

func (h Header) Compare(hh http.Header) error

TODO:(mkopriva) check all values of a field not just the first one. Compare checks if all of the receiver's key-value pairs are present in the specified http.Header returning an error if not.

type Hit

type Hit struct {
	// the endpoint to be tested
	Path string

	// the requests to be made to the above specified endpoint
	Requests Requests
}

Hit represents a bunch of test requests against a specific endpoint.

func (Hit) Test

func (h Hit) Test(t *testing.T)

Test executes all of the Hit's Requests.

type JSONBody

type JSONBody map[string]interface{}

JSONBody represents an http request body whose content is of type application/json.

func (JSONBody) Body

func (b JSONBody) Body() (io.Reader, error)

Body implements the Bodyer interface by marshaling the receiver's contents into a JSON string and returning it as an io.Reader.

func (JSONBody) Compare

func (b JSONBody) Compare(r io.Reader) error

Compare compares the receiver's contents to the contents of the specified reader.

func (JSONBody) Type

func (b JSONBody) Type() string

Type returns JSONBody's media type.

type MultipartBody

type MultipartBody map[string][]interface{}

MultipartBody represents an http request body whose content is of type multipart/form-data. The MultipartBody can handle values only of type string or hit's File.

func (MultipartBody) Body

func (b MultipartBody) Body() (io.Reader, error)

Body implements the Bodyer interface by serializing the receiver's contents into a mutlipart data stream and returning it as an io.Reader.

func (MultipartBody) Type

func (MultipartBody) Type() string

Type returns the MultipartBody's media type.

type Request

type Request struct {
	Skip   bool
	Header Header
	Body   Bodyer
	Want   Response
}

Request represents an HTTP request with its expected response.

func (Request) Execute

func (r Request) Execute(method, path string) error

Execute prepares and executes an HTTP request with the specified method to the speciefied path.

type Requests

type Requests map[string][]Request

The type Requests maps HTTP methods to Request slices.

func (Requests) Skip

func (rs Requests) Skip() Requests

Skip marks all the Requests' members to be skipped by the Hit when the next test is executed.

type Response

type Response struct {
	Status int
	Header Header
	Body   JSONBody
}

Response represents a trimmed down HTTP response.

func (Response) Compare

func (r Response) Compare(res *http.Response) error

Compare compares the specified http.Repsonse to the receiver.

func (Response) CompareStatus

func (r Response) CompareStatus(status int) error

CompareStatus checks if the specified status is equal to the receiver's Status. If they are not equal a formatted error is returned.

Jump to

Keyboard shortcuts

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