hofstadter-io / frisby

API testing framework inspired by frisby-js
MIT License
278 stars 27 forks source link
api-testing-framework frisby frisby-js golang testing

frisby

Build Status GoDoc GitHub release

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

Proposals

I'm starting to work on frisby again with the following ideas:

  1. Read specification files
    • pyresttest
    • frisby.js
    • swagger spec
    • other?
  2. Use as a load tester
    • like Locust.io
    • distributed
  3. UI
    • Dashboard
    • Analytics
    • Reports
    • Manage multiple instances
  4. Backend
    • master/minions
    • db for analytics
    • api for UI / clients Goa
    • federation of minion groups?

Please comment on any issues or PRs related to these proposals. If you don't see an issue, PR, or idea; definitely add it!

Installation

go get -u github.com/verdverm/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:

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)

Post-flight functions

Functions called after Send()

More examples

You can find a longer example here

package main

import (
    "fmt"
    "reflect"

    "github.com/bitly/go-simplejson"
    "github.com/verdverm/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!