Skip to content

A Mock HTTP Client package for efficient testing of REST Client response handling scenarios

License

Notifications You must be signed in to change notification settings

subbuv26/mockhttpclient

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GoDoc Go Report Card License

Mock HTTP Client

A Mock HTTP Client package for efficient testing of REST Client response handling scenarios. Mock HTTP client acts as an HTTP client and serves requests as per the needs of tests. This mock HTTP client simply responds with preconfigured HTTP responses when ever http requests are made.

To test the code that handles different http responses, this mock client comes very useful, as the client responds with desired responses, the code that gets tested receives the expected responses in the expected order.

Installation

go get github.com/subbuv26/mockhttpclient

Usage

Example 1:

The function to be tested (createAndVerifyResource) has the below functionality

  1. CREATE resource using POST call, which returns http OK
  2. GET the resource using GET call, which returns http Service Unavailable (Server Busy)
  3. retry to GET the resource, which returns http OK
  4. On Success return true, otherwise false
import mockhc "github.com/subbuv26/mockhttpclient"

resp1 := &http.Response{
    StatusCode: 200,
    Header:     http.Header{},
    Body:       ioutil.NopCloser(bytes.NewReader([]byte("body"))),
}

resp2 := &http.Response{
    StatusCode: 503,
    Header:     http.Header{},
    Body:       ioutil.NopCloser(bytes.NewReader([]byte("body"))),
}

responseMap := make(mockhc.ResponseConfigMap)

responseMap[http.MethodPost] = &ResponseConfig{}
responseMap[http.MethodPost].Responses = []*http.Response{resp1}
responseMap[http.MethodGet] = &ResponseConfig{}
responseMap[http.MethodGet].Responses = []*http.Response{resp2, resp1}

// Create Client
client, _ := NewMockHTTPClient(responseMap)

// myRESTClient is the client that is going to be tested
myRESTClient.client = client

// createAndVerifyResource creates a resource  with POST and verifies with GET
// with myRESTClient.client
ok := myRESTClient.createAndVerifyResource(Resource{})
if ok {
	// Success 
} else {
	// Failed
}

Example 2

The function to be tested (createResources) creates N number of resources by calling continuous POST calls

import mockhc "github.com/subbuv26/mockhttpclient"

N := 5

resp1 := &http.Response{
    StatusCode: 200,
    Header:     http.Header{},
    Body:       ioutil.NopCloser(bytes.NewReader([]byte("body"))),
}

responseMap := make(mockhc.ResponseConfigMap)

responseMap[http.MethodPost] = &ResponseConfig{}
responseMap[http.MethodPost].Responses = []*http.Response{resp1}
responseMap[http.MethodPost].MaxRun = N

// Create Client
client, _ := NewMockHTTPClient(responseMap)

// myRESTClient is the client that is going to be tested
myRESTClient.client = client

// createResources makes post calls using myRESTClient.client
ok := myRESTClient.createResources([]Resources)
if ok {
    // Success 
} else {
    // Failed
}

About

A Mock HTTP Client package for efficient testing of REST Client response handling scenarios

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages