Giter Site home page Giter Site logo

supertest's Introduction

Supertest - Library from write http tests

Go code (golang) set of packages that provide many tools for testifying that your code will behave as you intend.

Get started:

[supertest]

The supertest package provides some helpful methods that allow you to write better http test code in Go.

  • Prints friendly, easy to read failure descriptions
  • Allows for very readable code
  • Optionally annotate each assertion with a message

See it in action:

package yours

import (
  "testing"
  "github.com/IcaroSilvaFK/supertest"
)


func TestShouldRequestWithParams(t *testing.T) {

	tt := supertest.New()

	tt.Method("GET").Url("http://httpbin.org/get").Status(200).Build(t)

}
  • Every assert func takes the testing.T object as the first argument. This is how it writes the errors out through the normal go test capabilities.
package yours

import (
  "testing"
	"github.com/IcaroSilvaFK/supertest"
)

func TestShouldExpectHttpErrorNotFoundOnApiNotExists(t *testing.T) {

	tt := supertest.New()

	tt.Method("GET").Url("http://httpbin.org/status/404").Status(404).Build(t)
}

func TestShouldRequestBodyNotEmpty(t *testing.T) {

	var body struct {
		UserId    int    `json:"userId"`
		ID        int    `json:"id"`
		Title     string `json:"title"`
		Completed bool   `json:"completed"`
	}

	tt := supertest.New()

	tt.Method("GET").Url("https://jsonplaceholder.typicode.com/todos/1").Json(&body).Status(200).Build(t)

	assert.NotNil(t, body)
	assert.Equal(t, 1, body.ID)
	assert.Equal(t, "delectus aut autem", body.Title)
	assert.Equal(t, false, body.Completed)
	assert.Equal(t, 1, body.UserId)
}

func TestShouldRequestPostOnApi(t *testing.T) {

	tt := supertest.New()

	var res struct {
		Title  string `json:"title"`
		Body   string `json:"body"`
		UserId int    `json:"userId"`
	}

	r := []byte(`{"title": "foo", "body": "bar", "userId": 1}`)

	tt.Method("POST").Url("https://jsonplaceholder.typicode.com/todos").Body(r).Json(&res).Status(201).Build(t)

	assert.Equal(t, "foo", res.Title)
	assert.Equal(t, "bar", res.Body)
	assert.Equal(t, 1, res.UserId)
}

func TestShouldRequestNotPostOnRouteNotFound(t *testing.T) {

	tt := supertest.New()

	r := []byte(`{"title": "foo", "body": "bar", "userId": 1}`)

	tt.Method("POST").Url("https://jsonplaceholder.typicode.com/tod").Body(r).Status(404).Build(t)
}

func TestShouldUrlMatchOnAddQueryParams(t *testing.T) {

	tt := supertest.New()

	tt.Url("http://httpbin.org/get").Query(map[string]string{"foo": "bar", "baz": "qux", "key": "value"})

	url := tt.GetUrl()

	assert.Equal(t, "http://httpbin.org/get?foo=bar&baz=qux&key=value", url)
}

func TestShouldValidateBody(t *testing.T) {

	var body struct {
		UserId    int    `json:"userId" validate:"required"`
		ID        int    `json:"id" validate:"required"`
		Title     string `json:"title" validate:"required"`
		Completed bool   `json:"completed" validate:"required"`
	}

	tt := supertest.New()

	tt.Method("GET").Url("https://jsonplaceholder.typicode.com/todos/1").Json(&body).Status(200).ValidateBody().Build(t)
}

Supported go versions

We currently support the most recent major Go versions from 1.22 onward.


Contributing

Please feel free to submit issues, fork the repository and send pull requests!

When submitting an issue, we ask that you please include a complete test function that demonstrates the issue. Extra credit for those using Testify to write the test code that demonstrates it.


License

This project is licensed under the terms of the MIT license.

Huge thanks to our contributors ❤️

Made with contrib.rocks.

supertest's People

Contributors

icarosilvafk avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.