Skip to content
This repository has been archived by the owner on Oct 27, 2020. It is now read-only.

Commit

Permalink
enhancing unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GRECO, FRANK committed Aug 9, 2017
1 parent e1709fa commit d5b9acf
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 9 deletions.
27 changes: 24 additions & 3 deletions steps/mockplugin_test.go → steps/mockplugins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
package steps

import (
"errors"
"context"
"net/http"

Expand All @@ -30,12 +31,32 @@ import (
"github.com/opentracing/opentracing-go"
)

type fakePlugin struct{}
type fakePanicPlugin struct{}

func (plugin fakePlugin) OnRequest(ctx context.Context, p spec.APIProxy, c controller.Controller, r *http.Request, span opentracing.Span) error {
func (plugin fakePanicPlugin) OnRequest(ctx context.Context, p spec.APIProxy, c controller.Controller, r *http.Request, span opentracing.Span) error {
panic("intentional")
}

func (plugin fakePlugin) OnResponse(ctx context.Context, p spec.APIProxy, c controller.Controller, r *http.Request, resp *http.Response, span opentracing.Span) error {
func (plugin fakePanicPlugin) OnResponse(ctx context.Context, p spec.APIProxy, c controller.Controller, r *http.Request, resp *http.Response, span opentracing.Span) error {
panic("intentional")
}

type fakeSuccessPlugin struct{}

func (plugin fakeSuccessPlugin) OnRequest(ctx context.Context, p spec.APIProxy, c controller.Controller, r *http.Request, span opentracing.Span) error {
return nil
}

func (plugin fakeSuccessPlugin) OnResponse(ctx context.Context, p spec.APIProxy, c controller.Controller, r *http.Request, resp *http.Response, span opentracing.Span) error {
return nil
}

type fakeErrorPlugin struct{}

func (plugin fakeErrorPlugin) OnRequest(ctx context.Context, p spec.APIProxy, c controller.Controller, r *http.Request, span opentracing.Span) error {
return errors.New("error")
}

func (plugin fakeErrorPlugin) OnResponse(ctx context.Context, p spec.APIProxy, c controller.Controller, r *http.Request, resp *http.Response, span opentracing.Span) error {
return errors.New("error")
}
5 changes: 3 additions & 2 deletions steps/pluginsonrequest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func TestPluginsOnRequestGetName(t *testing.T) {
}

func TestDoOnRequest(t *testing.T) {
err := doOnRequest(context.Background(), spec.APIProxy{}, controller.Controller{}, nil, opentracing.StartSpan("test span"), fakePlugin{})
assert.Equal(t, err.Error(), "OnRequest paniced")
assert.Equal(t, doOnRequest(context.Background(), spec.APIProxy{}, controller.Controller{}, nil, opentracing.StartSpan("test span"), fakePanicPlugin{}).Error(), "OnRequest paniced")
assert.Equal(t, doOnRequest(context.Background(), spec.APIProxy{}, controller.Controller{}, nil, opentracing.StartSpan("test span"), fakeErrorPlugin{}).Error(), "error")
assert.Nil(t, doOnRequest(context.Background(), spec.APIProxy{}, controller.Controller{}, nil, opentracing.StartSpan("test span"), fakeSuccessPlugin{}))
}
5 changes: 3 additions & 2 deletions steps/pluginsonresponse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func TestPluginsOnResponseGetName(t *testing.T) {
}

func TestDoOnResponse(t *testing.T) {
err := doOnResponse(context.Background(), spec.APIProxy{}, controller.Controller{}, nil, nil, opentracing.StartSpan("test span"), fakePlugin{})
assert.Equal(t, err.Error(), "OnResponse paniced")
assert.Equal(t, doOnResponse(context.Background(), spec.APIProxy{}, controller.Controller{}, nil, nil, opentracing.StartSpan("test span"), fakePanicPlugin{}).Error(), "OnResponse paniced")
assert.Equal(t, doOnResponse(context.Background(), spec.APIProxy{}, controller.Controller{}, nil, nil, opentracing.StartSpan("test span"), fakeErrorPlugin{}).Error(), "error")
assert.Nil(t, doOnResponse(context.Background(), spec.APIProxy{}, controller.Controller{}, nil, nil, opentracing.StartSpan("test span"), fakeSuccessPlugin{}))
}
33 changes: 31 additions & 2 deletions steps/writeresponse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,42 @@
package steps

import (
"bytes"
"testing"
"context"
"net/http"
"io/ioutil"
"net/http/httptest"

"github.com/stretchr/testify/assert"
opentracing "github.com/opentracing/opentracing-go"
)

func TestWriteResponseGetName(t *testing.T) {
assert := assert.New(t)
step := WriteResponseStep{}
assert.Equal(step.GetName(), "Write Response", "step name is incorrect")
assert.Equal(t, step.GetName(), "Write Response", "step name is incorrect")
}

func TestWriteResponseDo(t *testing.T) {
step := WriteResponseStep{}
writer := httptest.NewRecorder()
response := &httptest.ResponseRecorder{
Code: 200,
HeaderMap: http.Header{
"One": []string{"two"},
"Three": []string{"four"},
},
Body: bytes.NewBuffer([]byte("this is my mock response body")),
}
err := step.Do(context.Background(), nil, writer, nil, response.Result(), opentracing.StartSpan("test span"))
defer writer.Result().Body.Close()
assert.Nil(t, err)
assert.Equal(t, writer.Result().StatusCode, 200)
assert.Equal(t, writer.Result().Status, "OK")
assert.Equal(t, len(writer.Result().Header), 2)
assert.Equal(t, writer.Result().Header.Get("one"), "two")
assert.Equal(t, writer.Result().Header.Get("three"), "four")
bodyBytes, err := ioutil.ReadAll(writer.Result().Body)
assert.Nil(t, err)
assert.Equal(t, string(bodyBytes), "this is my mock response body")
}

0 comments on commit d5b9acf

Please sign in to comment.