Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validate transaction ID format #49

Merged
merged 4 commits into from
Jan 31, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
added real server test to cover invalid transaction IDs
  • Loading branch information
xh3b4sd committed Jan 31, 2017
commit e8eb88574fbaf3539ef48bd61b7280ed545cb15f
40 changes: 40 additions & 0 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"

kitendpoint "github.com/go-kit/kit/endpoint"
Expand Down Expand Up @@ -175,6 +176,45 @@ func Test_Transaction_IDGiven(t *testing.T) {
}
}

func Test_Transaction_InvalidIDGiven(t *testing.T) {
e := testNewEndpoint(t)

config := DefaultConfig()
config.Endpoints = []Endpoint{e}
config.ErrorEncoder = func(ctx context.Context, serverError error, w http.ResponseWriter) {
w.WriteHeader(http.StatusInternalServerError)
}
newServer, err := New(config)
if err != nil {
t.Fatal("expected", nil, "got", err)
}

newServer.Boot()
defer newServer.Shutdown()

// Here we make a request against our test endpoint. The endpoint is provided
// with an invalid transaction ID. The server's error encoder returns status
// code 500 on all errors.
{
r, err := http.NewRequest("GET", "/test-path", nil)
if err != nil {
t.Fatal("expected", nil, "got", err)
}
r.Header.Add(TransactionIDHeader, "--my-invalid-transaction-id--")
w := httptest.NewRecorder()

newServer.Router().ServeHTTP(w, r)

if w.Code != http.StatusInternalServerError {
t.Fatal("expected", http.StatusInternalServerError, "got", w.Code)
}

if !strings.Contains(w.Body.String(), "invalid transaction ID: does not match") {
t.Fatal("expected", "invalid transaction ID: does not match", "got", w.Body.String())
}
}
}

func testNewEndpoint(t *testing.T) Endpoint {
newEndpoint := &testEndpoint{
decoderExecuted: 0,
Expand Down