github

gavv / httpexpect

  • понедельник, 9 мая 2016 г. в 03:12:13
https://github.com/gavv/httpexpect

Go
Go module that helps to write nice tests for your HTTP API.



httpexpect GoDoc Travis-CI Coveralls

Go module that helps to write nice tests for your HTTP API.

Features

  • Incrementally build HTTP requests.
  • Inspect HTTP responses.
  • Inspect JSON payload, recursively (supported types: object, array, string, number, boolean, null).
  • By default, uses testify to report failures (can be configured to use assert or require package).
  • Configurable (accepts custom implementations of failure reporter, HTTP client, and logger).

Documentation

Documentation is available on GoDoc.

Installation

$ go get github.com/gavv/httpexpect

Example

See example directory for complete sources of fruits server and test.

import (
    "github.com/gavv/httpexpect"
    "net/http"
    "net/http/httptest"
    "testing"
)

func TestFruits(t *testing.T) {
    server := httptest.NewServer(FruitServer())
    defer server.Close()

    e := httpexpect.New(t, server.URL)

    e.GET("/fruits").
        Expect().
        Status(http.StatusOK).JSON().Array().Empty()

    orange := map[string]interface{}{
        "weight": 100,
    }

    e.PUT("/fruits/orange").WithJSON(orange).
        Expect().
        Status(http.StatusNoContent).NoContent()

    apple := map[string]interface{}{
        "colors": []interface{}{"green", "red"},
        "weight": 200,
    }

    e.PUT("/fruits/apple").WithJSON(apple).
        Expect().
        Status(http.StatusNoContent).NoContent()

    e.GET("/fruits").
        Expect().
        Status(http.StatusOK).JSON().Array().ElementsAnyOrder("orange", "apple")

    e.GET("/fruits/orange").
        Expect().
        Status(http.StatusOK).JSON().Object().Equal(orange).NotEqual(apple)

    e.GET("/fruits/orange").
        Expect().
        Status(http.StatusOK).
        JSON().Object().ContainsKey("weight").ValueEqual("weight", 100)

    obj := e.GET("/fruits/apple").
        Expect().
        Status(http.StatusOK).JSON().Object()

    obj.Keys().ElementsAnyOrder("colors", "weight")

    obj.Value("colors").Array().Elements("green", "red")
    obj.Value("colors").Array().Element(0).String().Equal("green")
    obj.Value("colors").Array().Element(1).String().Equal("red")

    obj.Value("weight").Number().Equal(200)

    e.GET("/fruits/melon").
        Expect().
        Status(http.StatusNotFound)
}

Similar modules

Contributing

Feel free to report bugs, suggest improvements, and send pull requests! Don't forget to add documentation and tests for new features and run all tests before submitting pull requests:

$ go test github.com/gavv/httpexpect/...

License

MIT