ujihttp

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2021 License: MIT Imports: 11 Imported by: 0

README

ujiHTTP

HTTP handler testing for golang web framework. The benchmark features used fasthttp.

Support Framework

Will support another framework for the future

Installation

go get github.com/KodepandaID/ujihttp

Example

This is an example of the main program using Gin, but you can also use net/http, Gorilla Mux, or Echo.

main.go
package main

import (
    "net/http"

    "github.com/KodepandaID/ujihttp"
    "github.com/gin-gonic/gin"
)

func GinEngine() *gin.Engine {
	r := gin.New()

	r.GET("/", func(c *gin.Context) {
		c.String(http.StatusOK, "Hello World!!")
    })

    return r
}

This is an example for testing the main program.

test.go
package main

import (
    "net/http"
    "testing"

    "github.com/KodepandaID/ujihttp"
    "github.com/stretchr/testify/assert"
)

func TestGinGET(t *testing.T) {
    r := ujihttp.New()

	r.
		GET("/").
		Run(GinEngine(), func(req *http.Request, rec *httptest.ResponseRecorder) {
			assert.Equal(t, http.StatusOK, rec.Code)
        })
}

Usage

SetDebug

To enable debug mode.

func main() {
    r := ujihttp.New()

    r.
        SetDebug(true).
        GET("/").
        Run(GinEngine())
}
METHOD

You can use the GET, POST, PUT, DELETE, PATCH, and HEAD method.

func main() {
    r := ujihttp.New()

	r.
        GET("/").
        Run(GinEngine())
}
SendJSON

If you use the POST or PUT method, you can send a JSON body.

func main() {
    r := ujihttp.New()

	r.
        POST("/add-json").
        SendJSON(ujihttp.JSON{
			"user":     "test",
			"password": "password",
		}).
        Run(GinEngine())
}
SendFormData

If you want to send data with header multipart/form-data

func main() {
    r := ujihttp.New()

	r.
        POST("/add-json").
        SendJSON(ujihttp.JSON{
			"user":     "test",
			"password": "password",
		}).
        Run(GinEngine())
}
SendFile

You can combine this method with SendFormData method.

func main() {
    r := ujihttp.New()
	path, _ := os.Getwd()

	r.
		SetDebug(true).
		POST("/post-file").
        SendFile("file", path+"/assets/sample.jpg").
        Run(GinEngine())
}
SendMultipleFile

If you want to upload multiple files, use this method. You can combine this method with SendFormData method.

func main() {
    r := ujihttp.New()
	path, _ := os.Getwd()

	r.
		SetDebug(true).
		POST("/post-file").
        SendMultipleFile("file", path+"/assets/sample.jpg").
        Run(GinEngine())
}

How to Benchmark

You can benchmark your API using this library. But, if you want to benchmark your API, make sure your API already run.

func main() {
    r := benchmark.New()
	r.
		Duration(10).
		Concurrent(500).
		Pipeline(1).
		GET("http://localhost:3000").
		Run()
}

See below the methods you can use on the Benchmark features.

Duration

You can set how long the benchmark run in the second.

r := benchmark.New()
r.Duration(10)
Concurrent

You can set how many the concurrent connection.

r := benchmark.New()
r.Concurrent(500)
Pipeline

You can set how many used the HTTP pipeline request.

r := benchmark.New()
r.Pipeline(500)
Methods

Same with the API testing handler, you can use GET, POST, PUT, DELETE, PATCH, and HEAD method.

r := benchmark.New()
r.GET("/").Run()

License

Copyright Yudha Pratama. Licensed under MIT.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type H

type H map[string]string

H is a HTTP map string data

type JSON

type JSON map[string]interface{}

JSON is HTTP map string interface data

type ReqConf

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

ReqConf is a request config to test api

func New

func New() *ReqConf

New to start api test handler

func (*ReqConf) DELETE

func (rc *ReqConf) DELETE(p string) *ReqConf

DELETE request method

func (*ReqConf) GET

func (rc *ReqConf) GET(p string) *ReqConf

GET request method

func (*ReqConf) HEAD

func (rc *ReqConf) HEAD(p string) *ReqConf

HEAD request method

func (*ReqConf) OPTIONS

func (rc *ReqConf) OPTIONS(p string) *ReqConf

OPTIONS request method

func (*ReqConf) PATCH

func (rc *ReqConf) PATCH(p string) *ReqConf

PATCH request method

func (*ReqConf) POST

func (rc *ReqConf) POST(p string) *ReqConf

POST request method

func (*ReqConf) PUT

func (rc *ReqConf) PUT(p string) *ReqConf

PUT request method

func (*ReqConf) Run

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

Run to start api test

func (*ReqConf) SendFile

func (rc *ReqConf) SendFile(fn, path string) *ReqConf

SendFile (fieldName, filepath string)

to send file from filepath

func (*ReqConf) SendFormData

func (rc *ReqConf) SendFormData(h H) *ReqConf

SendFormData to send multipart/form-data

func (*ReqConf) SendJSON

func (rc *ReqConf) SendJSON(j JSON) *ReqConf

SendJSON to send json data

func (*ReqConf) SendMultipleFile

func (rc *ReqConf) SendMultipleFile(fn string, path []string) *ReqConf

SendMultipleFile (fieldName, filepath string)

Use [] on the fieldname to send multiple file from filepath

func (*ReqConf) SetDebug

func (rc *ReqConf) SetDebug(b bool) *ReqConf

SetDebug to enable debug mode

func (*ReqConf) WithContentType

func (rc *ReqConf) WithContentType(ct string) *ReqConf

WithContentType to set content-type request

func (*ReqConf) WithCookies

func (rc *ReqConf) WithCookies(h H) *ReqConf

WithCookies to set cookies request

func (*ReqConf) WithHeader

func (rc *ReqConf) WithHeader(h H) *ReqConf

WithHeader to set header request

type ResponseFunc

type ResponseFunc func(*http.Request, *httptest.ResponseRecorder)

ResponseFunc response handling func type

Directories

Path Synopsis
pkg
cli

Jump to

Keyboard shortcuts

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