Skip to content

akm/go-testrequest

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

github.com/akm/go-testrequest

Overview

go-testrequest is a Go package that simplifies the creation and testing of HTTP requests. It provides a flexible and extensible way to build HTTP requests using a set of options and a factory pattern.

Installation

go get github.com/akm/go-testrequest

Usage

Creating Requests

You can create HTTP requests using the provided functions such as GET, POST, PUT, etc. Each function accepts a list of options to configure the request.

import (
    "github.com/akm/go-testrequest"
)

req := testrequest.GET(
    testrequest.BaseUrl("http://example.com"),
    testrequest.Path("/users"),
    testrequest.Header("Authorization", "Bearer token"),
)

Using Factories

Factories allow you to create requests with a set of default options. This is useful when you need to create multiple requests with the same base configuration.

factory := testrequest.NewFactory(testrequest.BaseUrl("http://example.com"))

req := factory.POST(
    testrequest.Path("/users"),
    testrequest.BodyString(`{"name":"John Doe"}`),
)

Example Test

Here is an example of how to use go-testrequest in a test:

package testrequest

import (
    "net/http"
    "testing"

    "github.com/akm/go-testrequest"
    "github.com/stretchr/testify/assert"
    "github.com/stretchr/testify/require"
)

func TestClientWithServer(t *testing.T) {
    testServer := startEchoServer(t)
    testServer.Start()
    defer testServer.Close()

    factory := testrequest.NewFactory(testrequest.BaseUrl(testServer.URL))

    client := &http.Client{}
    resp, err := client.Do(factory.GET(testrequest.Path("/foo")))
    require.NoError(t, err)
    defer resp.Body.Close()

    assert.Equal(t, http.StatusOK, resp.StatusCode)
}

This example demonstrates how to use go-testrequest to create and test HTTP requests in a Go test. See tests/client_test.go for more details.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published