apicontext

package module
v1.5.2-0...-79d9d49 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2021 License: MIT Imports: 19 Imported by: 0

README ΒΆ

godog-api-context

A set of reusable step definitions for testing REST APIs with Godog.

Go version Go Report Card CI Status Coverage Status

Commitizen friendly semantic-release

Pre-requisites

Usage

To recommended way is to integrate with Godog and Go test as specified in the Godog documentation

package main

import (
	"flag"
	"os"
	"testing"

	apicontext "github.com/brpaz/godog-api-context"
	"github.com/cucumber/godog"
)

var opts = godog.Options{
	Format:        "progress", // can define default values,
}

func init() {
	godog.BindFlags("godog.", flag.CommandLine, &opts)
}

func TestMain(m *testing.M) {
	flag.Parse()
	opts.Paths = flag.Args()

	apiContext := apicontext.New("<base_url>")

	status := godog.TestSuite{
		Name:                "godogs",
		ScenarioInitializer: apiContext.InitializeScenario,
		Options:             &opts,
	}.Run()

	if st := m.Run(); st > status {
		status = st
	}
	os.Exit(status)
}

You can see a complete example together with Feature files in examples folder.

Available step definitions

^I set query param "([^"]*)" with value "([^"]*)"$

^I set query params to:$

^I set header "([^"]*)" with value "([^"]*)"$

^I set headers to:$

^I send "([^"]*)" request to "([^"]*)" with form body::$

^I send "([^"]*)" request to "([^"]*)"$

^I send "([^"]*)" request to "([^"]*)" with body:$

^The response code should be (\d+)$

^The response should be a valid json$

^The response should match json:$

The response header "([^"]*)" should have value ([^"]*)$

^The response should match json schema "([^"]*)"$

^The json path "([^"]*)" should have value "([^"]*)"$

^wait for (\d+) seconds$

^Store data in scope variable "([^"]*)" with value ([^"]*)

^I store the value of response header "([^"]*)" as ([^"]*) in scenario scope$

^I store the value of body path "([^"]*)" as "([^"]*)" in scenario scope$

^The scenario variable "([^"]*)" should have value "([^"]*)"$

Scope Values

This can also store the values from http response body and header and then use in subsequent requests. To use the value of scope variable, use this pattern: `##(keyname)` without parenthesis

Example:

I store the value of response header "X-AUTH-TOKEN" as token in scenario scope
I set header "X-AUTH-TOKEN" with value "`##token`"

This can be used for Authentication headers.

Sample Feature files in examples/scope folder.

TODO

  • Add steps for setting Cookies

Contributing

Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

πŸ’› Support the project

If this project was useful to you in some form, I would be glad to have your support. It will help to keep the project alive and to have more time to work on Open Source.

The sinplest form of support is to give a ⭐️ to this repo.

You can also contribute with GitHub Sponsors.

GitHub Sponsors

Or if you prefer a one time donation to the project, you can simple:

Buy Me A Coffee

Author

πŸ‘€ Bruno Paz

πŸ“ License

Copyright Bruno Paz.

This project is MIT licensed.

Documentation ΒΆ

Overview ΒΆ

Package apicontext defines common step definitions for testing REST APIs using Cucumber and Godog

Index ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

This section is empty.

Functions ΒΆ

This section is empty.

Types ΒΆ

type ApiContext ΒΆ

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

ApiContext main struct

func New ΒΆ

func New(baseURL string) *ApiContext

New Creates a new instance of the API Context

func (*ApiContext) ISendRequestTo ΒΆ

func (ctx *ApiContext) ISendRequestTo(method, uri string) error

ISendRequestTo Sends a request to the specified endpoint using the specified method.

func (*ApiContext) ISendRequestToWithBody ΒΆ

func (ctx *ApiContext) ISendRequestToWithBody(method, uri string, requestBody *godog.DocString) error

ISendRequestToWithBody Send a request with json body. Ex: a POST request.

func (*ApiContext) ISendRequestToWithFormBody ΒΆ

func (ctx *ApiContext) ISendRequestToWithFormBody(method, uri string, requestBodyTable *godog.Table) error

ISendRequestToWithFormBody Send a request with json body. Ex: a POST request.

func (*ApiContext) ISetHeaderWithValue ΒΆ

func (ctx *ApiContext) ISetHeaderWithValue(name string, value string) error

ISetHeaderWithValue Step that add a new header to the current request.

func (*ApiContext) ISetHeadersTo ΒΆ

func (ctx *ApiContext) ISetHeadersTo(dt *godog.Table) error

ISetHeadersTo This step sets the request headers using a datatable as source. It allows to define multiple headers at the same time.

func (*ApiContext) ISetQueryParamWithValue ΒΆ

func (ctx *ApiContext) ISetQueryParamWithValue(name string, value string) error

ISetQueryParamWithValue Adds a new query param to the request

func (*ApiContext) ISetQueryParamsTo ΒΆ

func (ctx *ApiContext) ISetQueryParamsTo(dt *godog.Table) error

ISetQueryParamsTo Set query params from a Data Table

func (*ApiContext) InitializeScenario ΒΆ

func (ctx *ApiContext) InitializeScenario(s *godog.ScenarioContext)

InitializeScenario this function should be called when starting the Test suite, to register the available steps.

func (*ApiContext) ReplaceScopeVariables ΒΆ

func (ctx *ApiContext) ReplaceScopeVariables(data string) string

func (*ApiContext) StoreJsonPathValue ΒΆ

func (ctx *ApiContext) StoreJsonPathValue(pathExpr string, scopeKeyName string) error

StoreJsonPathValue Store value from json body path to scope map.

func (*ApiContext) StoreResponseHeader ΒΆ

func (ctx *ApiContext) StoreResponseHeader(name string, scopeKeyName string) error

StoreResponseHeader Store header value to scope map.

func (*ApiContext) StoreScopeData ΒΆ

func (ctx *ApiContext) StoreScopeData(scopeKeyName string, value string) error

StoreScopeData Store data in scope map.

func (*ApiContext) TheJSONPathHaveCount ΒΆ

func (ctx *ApiContext) TheJSONPathHaveCount(pathExpr string, expectedCount int) error

TheJSONPathHaveCount Validates if the field at the specified json path have the expected length

func (*ApiContext) TheJSONPathShouldBePresent ΒΆ

func (ctx *ApiContext) TheJSONPathShouldBePresent(pathExpr string) error

TheJSONPathShouldBePresent checks if the specified json path exists in the response body

func (*ApiContext) TheJSONPathShouldHaveValue ΒΆ

func (ctx *ApiContext) TheJSONPathShouldHaveValue(pathExpr string, expectedValue string) error

TheJSONPathShouldHaveValue Validates if the json object have the expected value at the specified path.

func (*ApiContext) TheJSONPathShouldMatch ΒΆ

func (ctx *ApiContext) TheJSONPathShouldMatch(pathExpr string, pattern string) error

TheJSONPathShouldMatch Validates Checks if the the value from the specified json path matches the specified pattern.

func (*ApiContext) TheResponseBodyShouldContain ΒΆ

func (ctx *ApiContext) TheResponseBodyShouldContain(s string) error

TheResponseBodyShouldContain Checks if the response body contains the specified string

func (*ApiContext) TheResponseBodyShouldMatch ΒΆ

func (ctx *ApiContext) TheResponseBodyShouldMatch(pattern string) error

TheResponseBodyMatch Checks if the response body matches the specified pattern

func (*ApiContext) TheResponseCodeShouldBe ΒΆ

func (ctx *ApiContext) TheResponseCodeShouldBe(statusCode int) error

TheResponseCodeShouldBe Check if the http status code of the response matches the specified value.

func (*ApiContext) TheResponseHeaderShouldHaveValue ΒΆ

func (ctx *ApiContext) TheResponseHeaderShouldHaveValue(name string, expectedValue string) error

TheResponseHeaderShouldHaveValue Verify the value of a response header

func (*ApiContext) TheResponseShouldBeAValidJSON ΒΆ

func (ctx *ApiContext) TheResponseShouldBeAValidJSON() error

TheResponseShouldBeAValidJSON checks if the response is a valid JSON.

func (*ApiContext) TheResponseShouldMatchJSON ΒΆ

func (ctx *ApiContext) TheResponseShouldMatchJSON(body *godog.DocString) error

TheResponseShouldMatchJSON Check that response matches the expected JSON.

func (*ApiContext) TheResponseShouldMatchJsonSchema ΒΆ

func (ctx *ApiContext) TheResponseShouldMatchJsonSchema(path string) error

TheResponseShouldMatchJsonSchema Checks if the response matches the specified JSON schema

func (*ApiContext) TheScopeVariableShouldHaveValue ΒΆ

func (ctx *ApiContext) TheScopeVariableShouldHaveValue(scopeKeyName string, expectedValue string) error

TheScopeVariableShouldHaveValue Verify the value of a scope variable

func (*ApiContext) WaitForSomeTime ΒΆ

func (ctx *ApiContext) WaitForSomeTime(timeToWait int) error

WaitForSomeTime halt for some time.

func (*ApiContext) WithBaseURL ΒΆ

func (ctx *ApiContext) WithBaseURL(url string) *ApiContext

WithBaseURL Configures context base URL

func (*ApiContext) WithDebug ΒΆ

func (ctx *ApiContext) WithDebug(debug bool) *ApiContext

WithDebug Configures debug mode

func (*ApiContext) WithJSONSchemasPath ΒΆ

func (ctx *ApiContext) WithJSONSchemasPath(path string) *ApiContext

WithJSONSchemasPath Specifies the path to JSON schema files for doing response validation

type ApiResponse ΒΆ

type ApiResponse struct {
	StatusCode  int
	Body        string
	ResponseObj *http.Response
}

ApiResponse Struct that wraps an API response. It contains common accessed fields like Status Code and the Payload as well as access to the raw http.Response object

Jump to

Keyboard shortcuts

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