featuretest

package module
v0.0.0-...-47aebd5 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2021 License: MIT Imports: 16 Imported by: 0

README

dp-go-featuretest

Library to help write feature-level tests against a REST api / microservice

Background

The intention of this library is to help when writing feature tests for a new or existing REST API. The library contains a set of useful helper feature steps to make writing new gherkin tests easy.

The steps in api_feature have been written as to be easily reusable when setting up tests against a REST API, and there are other additional parts of this library which can be plugged in to help test outputs of the tests e.g. setting up an in memory mongo to assert against changes to the database.

Installation

To install this package in your project simply run:

go get github.com/armakuni/dp-go-featuretest

This package works alongside the Godog BDD framework, to install this run:

go get github.com/cucumber/godog/cmd/godog@v0.11.0

NOTE: this library will eventually change ownership

Running tests

To set up and run tests using Godog, it is best to follow the instructions found here

To run Godog tests with the API testing features in this library all you need to do is update your root level test file to pass the http handler of your application to our NewAPIFeature, register the steps and add the reset function to the BeforeScenario function.

package main

import (
	featuretest "github.com/armakuni/dp-go-featuretest"
	"github.com/cucumber/godog"
)

func InitializeScenario(ctx *godog.ScenarioContext) {
	myAppFeature := NewMyAppFeature() // This is the part that YOU will implement
	apiFeature := featuretest.NewAPIFeature(myAppFeature.Handler)

	ctx.BeforeScenario(func(*godog.Scenario) {
		apiFeature.Reset()
	})
	ctx.AfterScenario(func(*godog.Scenario, error) {
	})

	apiFeature.RegisterSteps(ctx)
}

func InitializeTestSuite(ctx *godog.TestSuiteContext) {
	ctx.BeforeSuite(func() {
	})
}

Repository structure

The features that can be used all exist on the root level of the project.

The examples folder contains three different examples of how to use this library, each using different features and having a slightly different way of setting up.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type APIFeature

type APIFeature struct {
	ErrorFeature
	Initialiser ServiceInitialiser

	BeforeRequestHook func() error
	// contains filtered or unexported fields
}

APIFeature contains the information needed to test REST API requests

func NewAPIFeature

func NewAPIFeature(initialiser ServiceInitialiser) *APIFeature

NewAPIFeature returns a new APIFeature, takes a function to retrieve the bound handler just before a request is made

func NewAPIFeatureWithHandler

func NewAPIFeatureWithHandler(handler http.Handler) *APIFeature

NewAPIFeatureWithHandler create a new APIFeature with a handler already bound with your endpoints

func (*APIFeature) IAmAuthorised

func (f *APIFeature) IAmAuthorised() error

IAmAuthorised sets the Authorization header to a bogus token

func (*APIFeature) IAmNotAuthorised

func (f *APIFeature) IAmNotAuthorised() error

IAmNotAuthorised removes any Authorization header set in the request headers

func (*APIFeature) IGet

func (f *APIFeature) IGet(path string) error

IGet makes a get request to the provided path with the current headers

func (*APIFeature) IPostToWithBody

func (f *APIFeature) IPostToWithBody(path string, body *godog.DocString) error

IPostToWithBody makes a POST request to the provided path with the current headers and the body provided

func (*APIFeature) ISetTheHeaderTo

func (f *APIFeature) ISetTheHeaderTo(header, value string) error

ISetTheHeaderTo is a default step used to set a header and associated value for the next request

func (*APIFeature) IShouldReceiveTheFollowingJSONResponse

func (f *APIFeature) IShouldReceiveTheFollowingJSONResponse(expectedAPIResponse *godog.DocString) error

IShouldReceiveTheFollowingJSONResponse asserts that the response body and expected response body are equal

func (*APIFeature) IShouldReceiveTheFollowingJSONResponseWithStatus

func (f *APIFeature) IShouldReceiveTheFollowingJSONResponseWithStatus(expectedCodeStr string, expectedBody *godog.DocString) error

IShouldReceiveTheFollowingJSONResponseWithStatus asserts the response code and body match the expectation

func (*APIFeature) IShouldReceiveTheFollowingResponse

func (f *APIFeature) IShouldReceiveTheFollowingResponse(expectedAPIResponse *godog.DocString) error

IShouldReceiveTheFollowingResponse asserts the response body and expected response body are equal

func (*APIFeature) RegisterSteps

func (f *APIFeature) RegisterSteps(ctx *godog.ScenarioContext)

RegisterSteps binds the APIFeature steps to the godog context to enable usage in the feature tests

func (*APIFeature) Reset

func (f *APIFeature) Reset()

Reset the request headers

func (*APIFeature) TheHTTPStatusCodeShouldBe

func (f *APIFeature) TheHTTPStatusCodeShouldBe(expectedCodeStr string) error

TheHTTPStatusCodeShouldBe asserts that the status code of the response matches the expected code

func (*APIFeature) TheResponseHeaderShouldBe

func (f *APIFeature) TheResponseHeaderShouldBe(headerName, expectedValue string) error

TheResponseHeaderShouldBe asserts the response header matches the expectation

type AuthorizationFeature

type AuthorizationFeature struct {
	ErrorFeature
	FakeAuthService *httpfake.HTTPFake
}

func NewAuthorizationFeature

func NewAuthorizationFeature() *AuthorizationFeature

func (*AuthorizationFeature) Close

func (f *AuthorizationFeature) Close()

func (*AuthorizationFeature) RegisterSteps

func (f *AuthorizationFeature) RegisterSteps(ctx *godog.ScenarioContext)

func (*AuthorizationFeature) Reset

func (f *AuthorizationFeature) Reset()

type ErrorFeature

type ErrorFeature struct {
	// contains filtered or unexported fields
}

func (*ErrorFeature) Errorf

func (f *ErrorFeature) Errorf(format string, args ...interface{})

func (*ErrorFeature) StepError

func (f *ErrorFeature) StepError() error

type MongoFeature

type MongoFeature struct {
	Server   *memongo.Server
	Client   mongo.Client
	Database *mongo.Database
}

MongoFeature is a struct containing an in-memory mongo database

func NewMongoFeature

func NewMongoFeature(mongoOptions MongoOptions) *MongoFeature

NewMongoFeature creates a new in-memory mongo database using the supplied options

func (*MongoFeature) Close

func (m *MongoFeature) Close() error

Close stops the in-memory mongo database

func (*MongoFeature) RegisterSteps

func (m *MongoFeature) RegisterSteps(ctx *godog.ScenarioContext)

func (*MongoFeature) Reset

func (m *MongoFeature) Reset() error

Reset is currently not implemented

func (*MongoFeature) TheFollowingDocumentExistsInTheCollection

func (m *MongoFeature) TheFollowingDocumentExistsInTheCollection(collectionName string, document *godog.DocString) error

type MongoOptions

type MongoOptions struct {
	Port         int
	MongoVersion string
	// Logger         *log.Logger
	DatabaseName   string
	DownloadURL    string
	StartupTimeout time.Duration
}

MongoOptions contains a set of options required to create a new MongoFeature

type ServiceInitialiser

type ServiceInitialiser func() (http.Handler, error)

func StaticHandler

func StaticHandler(handler http.Handler) ServiceInitialiser

Directories

Path Synopsis
examples
api

Jump to

Keyboard shortcuts

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