gofight

package module
v2.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2016 License: MIT Imports: 11 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Gofight

GoDoc Build Status Coverage Status Go Report Card codebeat badge codecov

API Handler Testing for Golang framework.

Support Framework

Usage

Downladn this package.

$ go get gopkg.in/appleboy/gofight.v1

To import this package, add the following line to your code:

import "gopkg.in/appleboy/gofight.v1"

Usage

The following is basic testing example.

Main Program:

package main

import (
  "io"
  "net/http"
)

func BasicHelloHandler(w http.ResponseWriter, r *http.Request) {
  io.WriteString(w, "Hello World")
}

func BasicEngine() http.Handler {
  mux := http.NewServeMux()
  mux.HandleFunc("/", BasicHelloHandler)

  return mux
}

Testing:

package main

import (
  "github.com/appleboy/gofight"
  "github.com/stretchr/testify/assert"
  "net/http"
  "testing"
)

func TestBasicHelloWorld(t *testing.T) {
  r := gofight.New()

  r.GET("/").
    // turn on the debug mode.
    SetDebug(true).
    Run(BasicEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {

      assert.Equal(t, "Hello World", r.Body.String())
      assert.Equal(t, http.StatusOK, r.Code)
    })
}
Set Header

You can add custom header via SetHeader func.

func TestBasicHelloWorld(t *testing.T) {
  r := gofight.New()
  version := "0.0.1"

  r.GET("/").
    // turn on the debug mode.
    SetDebug(true).
    SetHeader(gofight.H{
      "X-Version": version,
    }).
    Run(BasicEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {

      assert.Equal(t, version, rq.Header.Get("X-Version"))
      assert.Equal(t, "Hello World", r.Body.String())
      assert.Equal(t, http.StatusOK, r.Code)
    })
}
POST FORM Data

Using SetFORM to generate form data.

func TestPostFormData(t *testing.T) {
  r := gofight.New()

  r.POST("/form").
    SetFORM(gofight.H{
      "a": "1",
      "b": "2",
    }).
    Run(BasicEngine(), func(r HTTPResponse, rq HTTPRequest) {
      data := []byte(r.Body.String())

      a, _ := jsonparser.GetString(data, "a")
      b, _ := jsonparser.GetString(data, "b")

      assert.Equal(t, "1", a)
      assert.Equal(t, "2", b)
      assert.Equal(t, http.StatusOK, r.Code)
    })
}
POST JSON Data

Using SetJSON to generate json data.

func TestPostJSONData(t *testing.T) {
  r := gofight.New()

  r.POST("/json").
    SetJSON(gofight.D{
      "a": 1,
      "b": 2,
    }).
    Run(BasicEngine, func(r HTTPResponse, rq HTTPRequest) {
      data := []byte(r.Body.String())

      a, _ := jsonparser.GetInt(data, "a")
      b, _ := jsonparser.GetInt(data, "b")

      assert.Equal(t, 1, int(a))
      assert.Equal(t, 2, int(b))
      assert.Equal(t, http.StatusOK, r.Code)
    })
}
POST RAW Data

Using SetBody to generate raw data.

func TestPostRawData(t *testing.T) {
  r := gofight.New()

  r.POST("/raw").
    SetBody("a=1&b=1").
    Run(BasicEngine, func(r HTTPResponse, rq HTTPRequest) {
      data := []byte(r.Body.String())

      a, _ := jsonparser.GetString(data, "a")
      b, _ := jsonparser.GetString(data, "b")

      assert.Equal(t, "1", a)
      assert.Equal(t, "2", b)
      assert.Equal(t, http.StatusOK, r.Code)
    })
}
Set Query String

Using SetQuery to generate raw data.

func TestQueryString(t *testing.T) {
  r := gofight.New()

  r.GET("/hello").
    SetQuery(gofight.H{
      "a": "1",
      "b": "2",
    }).
    Run(BasicEngine, func(r HTTPResponse, rq HTTPRequest) {
      assert.Equal(t, http.StatusOK, r.Code)
    })
}

or append exist query parameter.

func TestQueryString(t *testing.T) {
  r := gofight.New()

  r.GET("/hello?foo=bar").
    SetQuery(gofight.H{
      "a": "1",
      "b": "2",
    }).
    Run(BasicEngine, func(r HTTPResponse, rq HTTPRequest) {
      assert.Equal(t, http.StatusOK, r.Code)
    })
}

Using SetCookie to generate raw data.

func TestQueryString(t *testing.T) {
  r := gofight.New()

  r.GET("/hello").
    SetCookie(gofight.H{
      "foo": "bar",
    }).
    Run(BasicEngine, func(r HTTPResponse, rq HTTPRequest) {
      assert.Equal(t, http.StatusOK, r.Code)
      assert.Equal(t, "foo=bar", rq.Header.Get("cookie"))
    })
}

Example

Testing

$ make

License

Copyright 2016 Bo-Yi Wu @appleboy.

Licensed under the MIT License.

Documentation

Overview

Package gofight offers simple API http handler testing for Golang framework.

Details about the gofight project are found in github page:

https://github.com/appleboy/gofight

Installation:

$ go get -u github.com/appleboy/gofight

Set Header: You can add custom header via SetHeader func.

SetHeader(gofight.H{
  "X-Version": version,
})

Set Cookie: You can add custom cookie via SetCookie func.

SetCookie(gofight.H{
  "foo": "bar",
})

Set query string: Using SetQuery to generate query string data.

SetQuery(gofight.H{
  "a": "1",
  "b": "2",
})

POST FORM Data: Using SetForm to generate form data.

SetForm(gofight.H{
  "a": "1",
  "b": "2",
})

POST JSON Data: Using SetJSON to generate json data.

SetJSON(gofight.H{
  "a": "1",
  "b": "2",
})

POST RAW Data: Using SetBody to generate raw data.

SetBody("a=1&b=1")

For more details, see the documentation and example.

Index

Constants

View Source
const (
	Version         = "1.0"
	UserAgent       = "User-Agent"
	ContentType     = "Content-Type"
	ApplicationJSON = "application/json"
	ApplicationForm = "application/x-www-form-urlencoded"
)

Media types

Variables

This section is empty.

Functions

func TestRequest

func TestRequest(t *testing.T, url string)

TestRequest is testing url string if server is running

Types

type D

type D map[string]interface{}

D is HTTP Data Type

type H

type H map[string]string

H is HTTP Header Type

type HTTPRequest

type HTTPRequest *http.Request

HTTPRequest is basic HTTP request type

type HTTPResponse

type HTTPResponse *httptest.ResponseRecorder

HTTPResponse is basic HTTP response type

type RequestConfig

type RequestConfig struct {
	Method  string
	Path    string
	Body    string
	Headers H
	Cookies H
	Debug   bool
}

RequestConfig provide user input request structure

func New

func New() *RequestConfig

New supply initial structure

func (*RequestConfig) DELETE

func (rc *RequestConfig) DELETE(path string) *RequestConfig

DELETE is request method.

func (*RequestConfig) GET

func (rc *RequestConfig) GET(path string) *RequestConfig

GET is request method.

func (*RequestConfig) HEAD

func (rc *RequestConfig) HEAD(path string) *RequestConfig

HEAD is request method.

func (*RequestConfig) OPTIONS

func (rc *RequestConfig) OPTIONS(path string) *RequestConfig

OPTIONS is request method.

func (*RequestConfig) PATCH

func (rc *RequestConfig) PATCH(path string) *RequestConfig

PATCH is request method.

func (*RequestConfig) POST

func (rc *RequestConfig) POST(path string) *RequestConfig

POST is request method.

func (*RequestConfig) PUT

func (rc *RequestConfig) PUT(path string) *RequestConfig

PUT is request method.

func (*RequestConfig) Run

func (rc *RequestConfig) Run(r http.Handler, response ResponseFunc)

Run execute http request

func (*RequestConfig) SetBody

func (rc *RequestConfig) SetBody(body string) *RequestConfig

SetBody supply raw body.

func (*RequestConfig) SetCookie added in v1.0.4

func (rc *RequestConfig) SetCookie(cookies H) *RequestConfig

SetCookie supply cookies what you defined.

func (*RequestConfig) SetDebug

func (rc *RequestConfig) SetDebug(enable bool) *RequestConfig

SetDebug supply enable debug mode.

func (*RequestConfig) SetForm added in v1.0.3

func (rc *RequestConfig) SetForm(body H) *RequestConfig

SetForm supply form body.

func (*RequestConfig) SetHeader

func (rc *RequestConfig) SetHeader(headers H) *RequestConfig

SetHeader supply http header what you defined.

func (*RequestConfig) SetJSON

func (rc *RequestConfig) SetJSON(body D) *RequestConfig

SetJSON supply JSON body.

func (*RequestConfig) SetQuery added in v1.0.3

func (rc *RequestConfig) SetQuery(query H) *RequestConfig

SetQuery supply query string.

type ResponseFunc

type ResponseFunc func(HTTPResponse, HTTPRequest)

ResponseFunc response handling func type

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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