frisby

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2020 License: MIT Imports: 11 Imported by: 0

README

frisby

REST API testing framework inspired by frisby-js, written in Go

Forked from https://github.com/verdverm/frisby

Proposals (Historical)
  1. Read specification files
  • pyresttest
  • frisby.js
  • swagger spec
  • other?
  1. Use as a load tester
  • like Locust.io
  • distributed
  1. UI
  • Dashboard
  • Analytics
  • Reports
  • Manage multiple instances
  1. Backend
  • master/minions
  • db for analytics
  • api for UI / clients Goa
  • federation of minion groups?
Installation
go get -u github.com/EducationPlannerBC/frisby
Basic Usage

First create a Frisby object:

// create an object with a given name (used in the report)
F := frisby.Create("Test successful user login").
    Get("https://golang.org")

Add any pre-flight data

F.SetHeader("Content-Type": "application/json").
	SetHeader("Accept", "application/json, text/plain, */*").
	SetJSON([]string{"item1", "item2", "item3"})

There is also a Global object for setting repeated Pre-flight options.

frisby.Global.BasicAuth("username", "password").
	SetHeader("Authorization", "Bearer " + TOKEN)

Next send the request:

F.Send()

Then assert and inspect the response:

F.ExpectStatus(200).
    ExpectJSON("nested.path.to.value", "sometext").
    ExpectJSON("nested.path.to.object", golangObject).
    ExpectJSON("nested.array.7.id", 23).
    ExpectJSONLength("nested.array", 8).
    AfterJSON(func(F *frisby.Frisby, json *simplejson.Json, err error) {
		val, _ := json.Get("proxy").String()
		frisby.Global.SetProxy(val)
	})

Finally, print out a report of the tests

frisby.Global.PrintReport()

Check any error(s), however the global report prints any that occured as well

err := F.Error()

errs := F.Errors()
for _,e := range errs {
	fmt.Println("Error: ", e)
}
HTTP Method functions

Your basic HTTP verbs:

  • Get(url string)
  • Post(url string)
  • Put(url string)
  • Patch(url string)
  • Delete(url string)
  • Head(url string)
  • Options(url string)
Pre-flight functions

Functions called before Send()

You can also set theses on the frisby.Global object for persisting state over multiple requests.

( Most of these come from github.com/mozillazg/request)

  • BasicAuth(username,password string)
  • Proxy(url string)
  • SetHeader(key,value string)
  • SetHeaders(map[string]string)
  • SetCookies(key,value string)
  • SetCookiess(map[string]string)
  • SetDate(key,value string)
  • SetDates(map[string]string)
  • SetParam(key,value string)
  • SetParams(map[string]string)
  • SetJSON(interface{})
  • SetFile(filename string)
Post-flight functions

Functions called after Send()

  • ExpectStatus(code int)
  • ExpectHeader(key, value string)
  • ExpectContent(content string)
  • ExpectJSON(path string, value interface{})
  • ExpectJSONLength(path string, length int)
  • ExpectJSONType(path string, value_type reflect.Kind)
  • AfterContent( func(Frisby,[]byte,error) )
  • AfterText( func(Frisby,string,error) )
  • AfterJSON( func(Frisby,simplejson.Json,error) )
  • PauseTest(t time.Duration)
  • PrintBody()
  • PrintReport()
  • PrintGoTestReport()
More examples

You can find a longer example here

package main

import (
	"fmt"
	"reflect"

	"github.com/bitly/go-simplejson"
	"github.com/EducationPlannerBC/frisby"
)

func main() {
	fmt.Println("Frisby!\n")

	frisby.Create("Test GET Go homepage").
		Get("http://golang.org").
		Send().
		ExpectStatus(200).
		ExpectContent("The Go Programming Language")

	frisby.Create("Test GET Go homepage (which fails)").
		Get("http://golang.org").
		Send().
		ExpectStatus(400).
		ExpectContent("A string which won't be found")

	frisby.Create("Test POST").
		Post("http://httpbin.org/post").
		SetData("test_key", "test_value").
		Send().
		ExpectStatus(200)

	frisby.Create("Test ExpectJSONType").
		Post("http://httpbin.org/post").
		Send().
		ExpectStatus(200).
		ExpectJSONType("url", reflect.String)

	frisby.Create("Test ExpectJSON").
		Post("http://httpbin.org/post").
		Send().
		ExpectStatus(200).
		ExpectJSON("url", "http://httpbin.org/post").
		ExpectJSON("headers.Accept", "*/*")

	frisby.Create("Test ExpectJSONLength (which fails)").
		Post("http://httpbin.org/post").
		SetJSON([]string{"item1", "item2", "item3"}).
		Send().
		ExpectStatus(200).
		ExpectJSONLength("json", 4)

	frisby.Create("Test AfterJSON").
		Post("http://httpbin.org/post").
		Send().
		ExpectStatus(200).
		AfterJSON(func(F *frisby.Frisby, json *simplejson.Json, err error) {
		val, _ := json.Get("url").String()
		frisby.Global.SetProxy(val)
	})

	frisby.Global.PrintReport()
}

Sample Output

Frisby!

.......
For 7 requests made
  FAILED  [3/13]
      [Test ExpectJSONLength]
        -  Expect length to be 4, but got 3
      [Test GET Go homepage (which fails)]
        -  Expected Status 400, but got 200: "200 OK"
        -  Expected Body to contain "A string which won't be found", but it was missing

catch!

Documentation

Index

Examples

Constants

View Source
const DefaultPathSeparator = "."

DefaultPathSeparator is the default path separator

Variables

View Source
var Global globalData

Global stores information used in multiple requests

Functions

This section is empty.

Types

type AfterContentFunc

type AfterContentFunc func(F *Frisby, content []byte, err error)

AfterContentFunc function type used as argument to AfterContent()

type AfterJSONFunc

type AfterJSONFunc func(F *Frisby, json *simplejson.Json, err error)

AfterJSONFunc function type used as argument to AfterJSON()

type AfterTextFunc

type AfterTextFunc func(F *Frisby, text string, err error)

AfterTextFunc function type used as argument to AfterText()

type ExpectFunc

type ExpectFunc func(F *Frisby) (bool, string)

ExpectFunc function type used as argument to Expect()

type Frisby

type Frisby struct {
	Name   string
	URL    string
	Method string

	Req           *request.Request
	Resp          *request.Response
	Errs          []error
	ExecutionTime float64
}

Frisby holds request/response

func Create

func Create(name string) *Frisby

Create creates a new Frisby object with the given name.

The given name will be used if you call PrintReport()

func (*Frisby) AddError

func (F *Frisby) AddError(errStr string) *Frisby

AddError manually adds an error, if you need to

func (*Frisby) AddFile

func (F *Frisby) AddFile(filename string) *Frisby

AddFile adds a file to the Form data for the coming request

func (*Frisby) AddFileByKey

func (F *Frisby) AddFileByKey(key, filename string) *Frisby

AddFileByKey adds a file to the Form data for the coming request

func (*Frisby) AfterContent

func (F *Frisby) AfterContent(foo AfterContentFunc) *Frisby

AfterContent allows you to write your own functions for inspecting the body of the response. You are also provided with the Frisby object.

The function signiture is AfterContentFunc

type AfterContentFunc func(F *Frisby, content []byte, err error)

func (*Frisby) AfterJSON

func (F *Frisby) AfterJSON(foo AfterJSONFunc) *Frisby

AfterJSON allows you to write your own functions for inspecting the body of the response. You are also provided with the Frisby object.

The function signiture is AfterJSONFunc

type AfterJSONFunc func(F *Frisby, json *simplejson.Json, err error)

simplejson docs: https://github.com/bitly/go-simplejson

Example
frisby.Create("Test AfterJSON").
	Post("http://httpbin.org/post").
	Send().
	ExpectStatus(200).
	AfterJSON(func(F *frisby.Frisby, json *simplejson.Json, err error) {
		val, _ := json.Get("url").String()
		fmt.Println("url =", val)
	})
Output:

url = http://httpbin.org/post

func (*Frisby) AfterText

func (F *Frisby) AfterText(foo AfterTextFunc) *Frisby

AfterText allows you to write your own functions for inspecting the body of the response. You are also provided with the Frisby object.

The function signiture is AfterTextFunc

type AfterTextFunc func(F *Frisby, text string, err error)

func (*Frisby) BasicAuth

func (F *Frisby) BasicAuth(user, passwd string) *Frisby

BasicAuth sets BasicAuth values for the coming request

func (*Frisby) Delete

func (F *Frisby) Delete(url string) *Frisby

Delete sets the HTTP method to DELETE for the given URL

func (*Frisby) Error

func (F *Frisby) Error() error

Error gets the most recent error for the Frisby object

This function should be called last

func (*Frisby) Errors

func (F *Frisby) Errors() []error

Errors gets all errors for the Frisby object

This function should be called last

func (*Frisby) Expect

func (F *Frisby) Expect(foo ExpectFunc) *Frisby

Expect checks according to the given function, which allows you to describe any kind of assertion.

func (*Frisby) ExpectContent

func (F *Frisby) ExpectContent(content string) *Frisby

ExpectContent checks the response body for the given string

func (*Frisby) ExpectHeader

func (F *Frisby) ExpectHeader(key, value string) *Frisby

ExpectHeader checks for header and if values match

func (*Frisby) ExpectJSON

func (F *Frisby) ExpectJSON(path string, value interface{}) *Frisby

ExpectJSON uses the reflect.DeepEqual to compare the response JSON and the supplied JSON for structural and value equality

path can be a dot joined field names. ex: 'path.to.subobject.field'

Example
package main

import (
	"github.com/EducationPlannerBC/frisby"
)

func main() {
	frisby.Create("Test ExpectJSON").
		Post("http://httpbin.org/post").
		Send().
		ExpectStatus(200).
		ExpectJSON("url", "http://httpbin.org/post").
		ExpectJSON("headers.Accept", "*/*").
		PrintReport()

}
Output:

Pass  [Test ExpectJSON]

func (*Frisby) ExpectJSONLength

func (F *Frisby) ExpectJSONLength(path string, length int) *Frisby

ExpectJSONLength checks if the JSON at path is an array and has the correct length

path can be a dot joined field names. ex: 'path.to.subobject.field'

func (*Frisby) ExpectJSONType

func (F *Frisby) ExpectJSONType(path string, valType reflect.Kind) *Frisby

ExpectJSONType checks if the types of the response JSON and the supplied JSON match

path can be a dot joined field names. ex: 'path.to.subobject.field'

Example
package main

import (
	"reflect"

	"github.com/EducationPlannerBC/frisby"
)

func main() {
	frisby.Create("Test ExpectJSONType").
		Post("http://httpbin.org/post").
		Send().
		ExpectStatus(200).
		ExpectJSONType("url", reflect.String).
		PrintReport()

}
Output:

Pass  [Test ExpectJSONType]

func (*Frisby) ExpectStatus

func (F *Frisby) ExpectStatus(code int) *Frisby

ExpectStatus checks the response status code

func (*Frisby) Get

func (F *Frisby) Get(url string) *Frisby

Get sets the HTTP method to GET for the given URL

Example
package main

import (
	"github.com/EducationPlannerBC/frisby"
)

func main() {
	frisby.Create("Test GET Go homepage").
		Get("http://golang.org").
		Send().
		ExpectStatus(200).
		ExpectContent("The Go Programming Language").
		PrintReport()

}
Output:

Pass  [Test GET Go homepage]

func (*Frisby) Head

func (F *Frisby) Head(url string) *Frisby

Head sets the HTTP method to HEAD for the given URL

func (*Frisby) Options

func (F *Frisby) Options(url string) *Frisby

Options sets the HTTP method to OPTIONS for the given URL

func (*Frisby) Patch

func (F *Frisby) Patch(url string) *Frisby

Patch sets the HTTP method to PATCH for the given URL

func (*Frisby) PauseTest

func (F *Frisby) PauseTest(t time.Duration) *Frisby

PauseTest pauses your testrun for a defined amount of seconds

func (*Frisby) Post

func (F *Frisby) Post(url string) *Frisby

Post sets the HTTP method to POST for the given URL

Example
package main

import (
	"github.com/EducationPlannerBC/frisby"
)

func main() {
	frisby.Create("Test POST").
		Post("http://httpbin.org/post").
		SetData("test_key", "test_value").
		Send().
		ExpectStatus(200).
		PrintReport()

}
Output:

Pass  [Test POST]

func (*Frisby) PrintBody

func (F *Frisby) PrintBody() *Frisby

PrintBody prints the body of the response

func (*Frisby) PrintGoTestReport

func (F *Frisby) PrintGoTestReport() *Frisby

PrintGoTestReport prints a report for the Frisby Object in go_test format

If there are any errors, they will all be printed as well

func (*Frisby) PrintReport

func (F *Frisby) PrintReport() *Frisby

PrintReport prints a report for the Frisby Object

If there are any errors, they will all be printed as well

Example
package main

import (
	"github.com/EducationPlannerBC/frisby"
)

func main() {
	frisby.Create("Test GET Go homepage").
		Get("http://golang.org").
		Send().
		ExpectStatus(400).
		ExpectContent("A string which won't be found").
		AddError("Manually Added Error").
		PrintReport()

}
Output:

FAIL  [Test GET Go homepage]
        -  Expected Status 400, but got 200: "200 OK"
        -  Expected Body to contain "A string which won't be found", but it was missing
        -  Manually Added Error

func (*Frisby) Put

func (F *Frisby) Put(url string) *Frisby

Put sets the HTTP method to PUT for the given URL

func (*Frisby) Send

func (F *Frisby) Send() *Frisby

Send the actual request to the URL

func (*Frisby) SetCookie

func (F *Frisby) SetCookie(key, value string) *Frisby

SetCookie sets a Cookie value for the coming request

func (*Frisby) SetCookies

func (F *Frisby) SetCookies(cookies map[string]string) *Frisby

SetCookies sets several Cookie values for the coming request

func (*Frisby) SetData

func (F *Frisby) SetData(key, value string) *Frisby

SetData sets a Form data for the coming request

func (*Frisby) SetDatas

func (F *Frisby) SetDatas(datas map[string]string) *Frisby

SetDatas sets several Form data for the coming request

func (*Frisby) SetHeader

func (F *Frisby) SetHeader(key, value string) *Frisby

SetHeader sets a Header value for the coming request

func (*Frisby) SetHeaders

func (F *Frisby) SetHeaders(headers map[string]string) *Frisby

SetHeaders sets several Headers for the coming request

func (*Frisby) SetJSON

func (F *Frisby) SetJSON(json interface{}) *Frisby

SetJSON sets the JSON body for the coming request

func (*Frisby) SetParam

func (F *Frisby) SetParam(key, value string) *Frisby

SetParam sets a url Param for the coming request

func (*Frisby) SetParams

func (F *Frisby) SetParams(params map[string]string) *Frisby

SetParams sets several url Param for the coming request

func (*Frisby) SetProxy

func (F *Frisby) SetProxy(url string) *Frisby

SetProxy sets proxy URL for the coming request

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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