apitester

package module
v0.0.0-...-7b13089 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2020 License: MIT Imports: 8 Imported by: 0

README

apitester : API testing package

Overview GoDoc

Package to unittest test golang API with json request and responses.

Install

go get github.com/akash1729/apitester

Example

package usage

import (
	"testing"

	"github.com/akash1729/apitester"
)

func GetUserTests(t *testing.T) {

	testCase := apitester.TestCase{
		TestName:    "CreateUser",
		TestDetail:  "Proper Implemetaion",
		Route:       "/User",
		Method:      "POST",
		HandlerFunc: CreateUser, //import and assign corresponding handler
		StatusCode:  200,
		AvoidKey:    []string{"token"},
		RequestHeader: map[string]string{
			"Authorization": "authorization key",
		},
		RequestMap: map[string]interface{}{
			"username": "nitya",
			"password": "password"},
		ResponseHeader: map[string]string{
			"Content-Type": "application/json",
		},
		ResponseMap: map[string]interface{}{
			"status":   "User Created succesfully",
			"userID":   88,
			"username": "nitya"},
		TypeCheck: map[string]interface{}{
			"token":    "stringType",
			"userID":   0,
			"username": "stringType",
		},
		RequestContextKey:   "requestID",
		RequestContextValue: 123,
	}

	apitester.RunTest(&testCase, t)

}

Only HandlerFunc Field is mandatory All other fields are optional during testing

Author

Akash Thomas

License

MIT.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RunTest

func RunTest(testCase *TestCase, t *testing.T) error

RunTest To run the test call RunTest with the test case and testing package pointer

Types

type TestCase

type TestCase struct {
	TestName            string                                       // name of the test, eg: GET Person
	TestDetail          string                                       // case which is being tested, eg: Person with invalid DOB
	Route               string                                       // Route, eg: /Person
	Method              string                                       // HTTP method, eg: POST
	HandlerFunc         func(w http.ResponseWriter, r *http.Request) // handler function
	StatusCode          int                                          // expected return status code
	AvoidKey            []string                                     // Keys with dynamic values like token or timestamp, eg: ["token"]
	RequestHeader       map[string]string                            // HTTP request header in key value pair map
	RequestMap          map[string]interface{}                       // Request data that can be marshaled into json
	ResponseHeader      map[string]string                            // HTTP response header in key value pair
	ResponseMap         map[string]interface{}                       // Response map that is unmarshaled from a json
	TypeCheck           map[string]interface{}                       // Values for type check, only the types of values are compared. For testing values like token
	RequestContextKey   interface{}                                  // Key value for context if request has context with values. Context can be used to test controllers where request already has a value
	RequestContextValue interface{}                                  // Value for context

}

TestCase Create a test case by creating instance of this struct. This structure is currently based on only json requests and responses

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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