supertest

package module
v0.0.0-...-a91f0ee Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2024 License: MIT Imports: 7 Imported by: 0

README

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.

Documentation

Overview

This package is a library for testing http requests Inspired by supertest on JavaScript ecosystem

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Tester

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

func (*Tester) Body

func (tt *Tester) Body(bt []byte) TesterInterface

Using the function from set body from http request

instance.Body([]byte(`{"title": "foo", "body": "bar", "userId": 1}`))

This return an instance of Tester

func (*Tester) Build

func (tt *Tester) Build(t *testing.T) *Tester

This return an instance of Tester expected testing

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

This return an instance of Tester

func (*Tester) GetBody

func (tt *Tester) GetBody() interface{}

func (*Tester) GetHeaders

func (tt *Tester) GetHeaders() map[string]string

func (*Tester) GetResponse

func (tt *Tester) GetResponse() *http.Response

func (*Tester) GetStatus

func (tt *Tester) GetStatus() int

func (*Tester) GetUrl

func (tt *Tester) GetUrl() string

GetUrl return the url of http request

instance.GetUrl() == "http://httpbin.org/get"

func (*Tester) Headers

func (tt *Tester) Headers(h map[string]string) TesterInterface

Using the function from set headers from http request the default header as ben set is Content-Type: application/json instance.Headers(map[string]string{"Content-Type": "application/json"}) This return an instance of Tester

func (*Tester) Json

func (tt *Tester) Json(i interface{}) TesterInterface

Using the function from set body return from http request

instance.Json(&body)

This return an instance of Tester

func (*Tester) Method

func (tt *Tester) Method(m string) TesterInterface

Using the function from set method from http request

instance.Method(http.MethodGet)

This return an instance of Tester

func (*Tester) Query

func (tt *Tester) Query(q map[string]string) TesterInterface

Using the function from set query from http request

instance.Query(map[string]string{"foo": "bar", "baz": "qux", "key": "value"})

This return an instance of Tester

func (*Tester) Status

func (tt *Tester) Status(s int) TesterInterface

Using the function from set status expected from http request

instance.Status(http.StatusOK)

This return an instance of Tester

func (*Tester) Url

func (tt *Tester) Url(url string) TesterInterface

Using the function from set url from http request

instance.Url("http://httpbin.org/get")

This return an instance of Tester

func (*Tester) ValidateBody

func (tt *Tester) ValidateBody() TesterInterface

type TesterInterface

type TesterInterface interface {
	Method(string) TesterInterface
	Url(string) TesterInterface
	Json(interface{}) TesterInterface
	Headers(map[string]string) TesterInterface
	Query(map[string]string) TesterInterface
	Body([]byte) TesterInterface
	Status(int) TesterInterface
	ValidateBody() TesterInterface
	GetUrl() string
	Build(*testing.T) *Tester
}

func New

func New() TesterInterface

This method return an instance of Tester

instance := supertest.NewHttpTester()

The snapshot is used to create a test builder

Jump to

Keyboard shortcuts

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