http

package
v0.8.6 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2023 License: MIT Imports: 8 Imported by: 0

README

HTTP utilities for godog test suits

A simple abstraction on top of the go http client.

Usage Example:

Feature:

Feature: It should be posible to create and retreive users from the service
    
    Scenario: If should be possible to create a user
        When a create user request with email "test-user@example.com", firstname "test-user" and lastname "example" is POSTED
        Then the returned status should be "200 OK"
        And the response header "Content-Type" should match "application/json"

    Scenario: If should be possible to get a user
        When a get user request with email "aladdin@example.com" is sent
        Then the returned status should be "200 OK"
        And the response header "Content-Type" should match "application/json"

Backing code:

import (
    "github.com/DATA-DOG/godog"
    "github.com/pkg/errors"
    "github.com/SKF/go-tests-utility/api/godog/json"
)

const (
    loginEmail = "aladdin@example.com"
    loginPassword = "simsalabim"
)

type state struct {
    client       *http.HttpClient
    requestURL    string
    requestBody   interface{}
    httpResponse *http.HttpResponse
    result        interface{}
}

type user struct {
    Email     string
    Firstname string
    Lastname  string
}

func (st *state) createUser(email, firstname, lastname string) error {
    st.requestURL = "https://localhost/users"
    st.requestBody = user{firstname, lastname, email} 
    resp, err := st.client.Post(st.requestURL, st.requestBody, nil)
    if err != nil {
        return err
    }
    st.httpResponse = resp
    return nil
}

func (st *state) getUser(email string) error {
    responseBody := struct {
        user user
    }{}

    resp, err := st.client.Get(st.requestURL, &responseBody)
    if err != nil {
        return err
    }

    st.httpResponse = resp
    st.result = responseBody.user
    return nil
}


func (st *state) httpStatusMatch(status string) error {
    if st.httpResponse == nil {
        return errors.Errorf("Expected a HTTP response")
    }

    if st.httpResponse.Status != status {
        return errors.Errorf("HTTP Status mismatch: expected %s got %s", status, st.httpResponse.Status)
    }

    return nil
}

func (st *state) httpHeaderMatch(header, pattern string) error {
    if st.httpResponse == nil {
        return errors.Errorf("Expected a HTTP response")
    }

    h, ok := st.httpResponse.Headers[header]
    if !ok {
        return errors.Errorf("Missing a %s HTTP header", header)
    }

    for _, v := range h {
        if v == pattern {
            return nil
        }
    }

    return errors.Errorf("No HTTP header value matched for %s (%+v)", header, h)
}

func FeatureContext(s *godog.Suite) {
    st := state{}
    st.client = http.New()

    if err := st.client.FetchToken("sandbox", loginEmail, loginPassword); err != nil {
        panic(err)
    }

    s.Step(`^a get user request with email "([^"]+)" is sent$`, st.getUser)
    s.Step(`^a create user request with email "([^"]+)", firstname "([^"]+)" and lastname "([^"]+)" is sent$`, st.createUser)
    s.Step(`^the returned status should be "([^"]+)"$`, st.httpStatusMatch)
    s.Step(`^the response header "([^"]+)" should match "([^"]+)"$`, st.httpHeaderMatch)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HttpClient

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

func New

func New() *HttpClient

func NewWithToken

func NewWithToken(token string) *HttpClient

func (*HttpClient) Delete

func (c *HttpClient) Delete(url string, out interface{}) (*HttpResponse, error)

func (*HttpClient) DeleteWithContext added in v0.0.8

func (c *HttpClient) DeleteWithContext(ctx context.Context, url string, out interface{}) (*HttpResponse, error)

func (*HttpClient) FetchToken

func (c *HttpClient) FetchToken(stage, username, password string) error

func (*HttpClient) FetchTokenWithContext added in v0.0.8

func (c *HttpClient) FetchTokenWithContext(ctx context.Context, stage, username, password string) error

func (*HttpClient) Get

func (c *HttpClient) Get(url string, out interface{}) (*HttpResponse, error)

func (*HttpClient) GetWithContext added in v0.0.8

func (c *HttpClient) GetWithContext(ctx context.Context, url string, out interface{}) (*HttpResponse, error)

func (*HttpClient) Post

func (c *HttpClient) Post(url string, in interface{}, out interface{}) (*HttpResponse, error)

func (*HttpClient) PostWithContext added in v0.0.8

func (c *HttpClient) PostWithContext(ctx context.Context, url string, in interface{}, out interface{}) (*HttpResponse, error)

func (*HttpClient) Put

func (c *HttpClient) Put(url string, in interface{}, out interface{}) (*HttpResponse, error)

func (*HttpClient) PutWithContext added in v0.0.8

func (c *HttpClient) PutWithContext(ctx context.Context, url string, in interface{}, out interface{}) (*HttpResponse, error)

type HttpResponse

type HttpResponse struct {
	Status     string
	StatusCode int
	Headers    map[string][]string
	Body       []byte
}

Jump to

Keyboard shortcuts

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