gostman

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2023 License: MIT Imports: 16 Imported by: 0

README

gostman Go Reference main

Postman nuances in Go Test.

Install

Just import gostman to your test package.

import github.com/minizilla/gostman

Usage

Always run the runtime in the TestMain.

func TestMain(m *testing.M) {
    os.Exit(gostman.Run(m))
}

Optional. Create gostman environment file .gostman.env.yml if using variable.

myenv:
  var1: "variable 1"
  var2: "variable 2"
another_env:
  var1: "another variable 1"
  var2: "another variable 2"

Create usual Test function TestXxx.

func TestRequest(t *testing.T) {
    gm := gostman.New(t)

    // every request run in the subtests
    gm.GET("Request", "url", func(r *gostman.Request) {
        // just code usual go code for Pre-request Script

        value := gm.V("var1") // get variable
        gm.SetV("var2", value) // set variable

        r.Params( /*sets Params here*/ )

        r.Authorization( /*sets Authorization here*/ )

        r.Headers( /*sets Headers here*/ )

        r.Body( /*sets Body here*/ )

        r.Send( /*send the request and tests the result here*/ )
    })

    // create another request
    gm.POST("Another Request", "url", func(r *gostman.Request) {})

Leverage go test command to run the above requests.

go test # run all gostman requests in the package
go test -run Request # run all collection in the TestRequest
go test -run Request/AnotherRequest # run only AnotherRequest
go test -run Request -env myenv # run request and use "myenv" environment
go test -run Request -setenv myenv # run request, use "myenv" environment and set it for the future request

The Runtime

Gostman generate file .gostman.runtime.yml to store runtime config and variable and change often after each request. It is recommended to ignore it in the .gitignore.

*.gostman.runtime.yml

Flags

  • All go test flags
  • -env {env} Select environment define in .gostman.env.yml
  • -setenv {env} Select environment define in .gostman.env.yml and set it for the future request
  • -reset Reset .gostman.runtime.yml
  • -debug Run gostman in debug mode

Example

More in the examples folder.

package gostman_test

import (
    "encoding/json"
    "net/http"
    "net/url"
    "os"
    "testing"

    "github.com/minizilla/gostman"
    "github.com/minizilla/testr"
)

func TestMain(m *testing.M) {
    os.Exit(gostman.Run(m))
}

func TestRequest(t *testing.T) {
    gm := gostman.New(t)

    gm.GET("Params", "https://postman-echo.com/get", func(r *gostman.Request) {
        r.Params(func(v url.Values) {
            v.Set("foo", "bar")
        })

        r.Send(func(t *testing.T, req *http.Request, res *http.Response) {
            defer res.Body.Close()

            assert := testr.New(t)
            assert.Equal(res.StatusCode, http.StatusOK)

            var resp = struct {
                Args map[string]string `json:"args"`
            }{}
            err := json.NewDecoder(res.Body).Decode(&resp)
            assert.ErrorIs(err, nil)
            assert.Equal(resp.Args["foo"], "bar")
        })
    })
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AuthAPIKey

func AuthAPIKey(key, val string) func(http.Header)

AuthAPIKey sets auth using api key.

func AuthBasic

func AuthBasic(username, password string) func(http.Header)

AuthBasic sets auth using basic username and password.

func AuthBearer

func AuthBearer(token string) func(http.Header)

AuthBearer sets auth using bearer token.

func BodyFormURLEncoded

func BodyFormURLEncoded(f func(url.Values)) (io.Reader, string, int)

BodyFormURLEncoded creates request body by encoding url values. Returns io.Reader, Content-Type of application/x-www-form-urlencoded, and its length.

func BodyJSON

func BodyJSON(v interface{}) (io.Reader, string, int)

BodyJSON creates request body by marshaling v using JSON. Returns io.Reader, Content-Type of application/json, and its length.

func BodyText

func BodyText(s string) (io.Reader, string, int)

Body Text creates request body using raw text. Returns io.Reader, Content-Type of text/plain, and its length.

func Run

func Run(m *testing.M) (code int)

Run run gostman runtime and the test. It returns an exit code to pass to os.Exit. The runtime should be run in the TestMain before using Gostman.

 func TestMain(m *testing.M) {
	 os.Exit(gostman.Run(m))
 }

Types

type Gostman

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

Gostman represents an API development set.

func New

func New(t *testing.T) *Gostman

New returns new Gostman.

func (*Gostman) DELETE

func (gm *Gostman) DELETE(name, url string, fn func(*Request))

DELETE run the request using DELETE method.

func (*Gostman) GET

func (gm *Gostman) GET(name, url string, fn func(*Request))

GET run the request using GET method.

func (*Gostman) HEAD

func (gm *Gostman) HEAD(name, url string, fn func(*Request))

HEAD run the request using HEAD method.

func (*Gostman) OPTIONS

func (gm *Gostman) OPTIONS(name, url string, fn func(*Request))

OPTIONS run the request using OPTIONS method.

func (*Gostman) PATCH

func (gm *Gostman) PATCH(name, url string, fn func(*Request))

PATCH run the request using PATCH method.

func (*Gostman) POST

func (gm *Gostman) POST(name, url string, fn func(*Request))

POST run the request using POST method.

func (*Gostman) PUT

func (gm *Gostman) PUT(name, url string, fn func(*Request))

PUT run the request using PUT method.

func (*Gostman) Request

func (gm *Gostman) Request(name, method, url string, fn func(*Request))

Request run the request.

func (*Gostman) SetV

func (gm *Gostman) SetV(name, val string)

SetV sets a variable.

func (*Gostman) V

func (gm *Gostman) V(name string) string

V returns a variable.

type Request

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

Request contains all necessary thing to create a Gostman request.

func (*Request) Authorization

func (r *Request) Authorization(f func(http.Header))

Authorization sets request authorization.

func (*Request) Body

func (r *Request) Body(body io.Reader, contentType string, contentLength int)

Body sets request body.

func (*Request) Headers

func (r *Request) Headers(f func(http.Header))

Headers sets request headers.

func (*Request) Params

func (r *Request) Params(f func(url.Values))

Params sets request params.

func (*Request) Send

func (r *Request) Send(f func(t *testing.T, req *http.Request, res *http.Response))

Send sends a request. The testing can be done inside f.

Jump to

Keyboard shortcuts

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