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

Add helper tests. #12

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
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
264 changes: 264 additions & 0 deletions helper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
package main

import (
"io/ioutil"
"net/http"
"net/http/httptest"
"reflect"
"testing"
)

func TestReturn(t *testing.T) {
val := &ValuerStruct{Code: http.StatusNotFound, Resp: "hello CORs!"}

rec := httptest.NewRecorder()
Return(rec, val)
resp := rec.Result()

expectedContentType := "application/json; charset=utf-8"

if resp.Header.Get("Content-Type") != expectedContentType {
t.Errorf("Incorrect header \"Content-Type\".\nExpected: %q\n Actual: %q\n", expectedContentType, resp.Header.Get("Content-Type"))
}

if resp.StatusCode != http.StatusNotFound {
t.Errorf("Incorrect HTTP status code.\nExpected: %v\n Actual: %q\n", http.StatusNotFound, resp.StatusCode)
}
}

func TestDefaultHeaders(t *testing.T) {
expectedOrigin := "origin-header"
expectedHeaders := map[string]string{
"Vary": "Origin",
"quote": "Be Happy :)",
"Access-Control-Allow-Origin": expectedOrigin,
"Access-Control-Allow-Credentials": "true",
}

req := httptest.NewRequest("OPTIONS", "/", nil)
req.Header.Set("Origin", expectedOrigin)

rec := httptest.NewRecorder()
defaultHeaders(rec, req)
resp := rec.Result()

for header, value := range expectedHeaders {
if resp.Header.Get(header) != value {
t.Errorf("Incorrect value for header %q.\nExpected: %q\n Actual: %q", header, value, resp.Header.Get(header))
}
}
}

func TestHeadersForPreflight(t *testing.T) {
expectedRequestMethod := "post"
expectedRequestHeaders := "X-PINGOTHER, Content-Type"
expectedVary := []string{"Authentication", "Access-Control-Request-Method", "Access-Control-Request-Headers"}

expectedHeaders := map[string]string{
"Access-Control-Allow-Methods": "POST",
"Access-Control-Allow-Headers": expectedRequestHeaders,
}

req := httptest.NewRequest("OPTIONS", "/", nil)
req.Header.Set("Access-Control-Request-Method", expectedRequestMethod)
req.Header.Set("Access-Control-Request-Headers", expectedRequestHeaders)

rec := httptest.NewRecorder()
rec.Header().Set("Vary", "Authentication")
headersForPreflight(rec, req)
resp := rec.Result()

actualVary := resp.Header["Vary"]
if !reflect.DeepEqual(actualVary, expectedVary) {
t.Errorf("Incorrect value for header %q.\nExpected: %q\n Actual: %q", "Vary", expectedVary, actualVary)
}

for header, value := range expectedHeaders {
if resp.Header.Get(header) != value {
t.Errorf("Incorrect value for header %q.\nExpected: %q\n Actual: %q", header, value, resp.Header.Get(header))
}
}
}

func TestAddHeaders(t *testing.T) {
tests := []struct {
Method string
RequestHeaders map[string]string
Origin string
Preflight bool
Vary []string
ResponseHeaders map[string]string
}{
{
"GET",
map[string]string{},
"origin-header-value-get",
false,
[]string{"Origin"},
map[string]string{
"Access-Control-Allow-Origin": "origin-header-value-get",
},
},
{
"OPTIONS",
map[string]string{
"Access-Control-Request-Method": "GET",
"Access-Control-Request-Headers": "Content-Type",
},
"origin-header-value-options",
true,
[]string{"Origin", "Access-Control-Request-Method", "Access-Control-Request-Headers"},
map[string]string{
"Access-Control-Allow-Origin": "origin-header-value-options",
},
},
}

constantHeaders := map[string]string{
"quote": "Be Happy :)",
"Access-Control-Allow-Credentials": "true",
}

for _, test := range tests {
t.Run(test.Method, func(t *testing.T) {
req := httptest.NewRequest(test.Method, "/", nil)
for h, v := range test.RequestHeaders {
req.Header.Set(h, v)
}

rec := httptest.NewRecorder()
isPreflight := addHeaders(rec, req)
if test.Preflight != isPreflight {
t.Errorf("Return value incorrect. Expected %v, actual %v.", test.Preflight, isPreflight)
}
resp := rec.Result()

for header, value := range constantHeaders {
if resp.Header.Get(header) != value {
t.Errorf("Incorrect value for header %q.\nExpected: %q\n Actual: %q", header, value, resp.Header.Get(header))
}
}

actualVary := resp.Header["Vary"]
if !reflect.DeepEqual(actualVary, test.Vary) {
t.Errorf("Incorrect value for header %q.\nExpected: %q\n Actual: %q", "Vary", test.Vary, actualVary)
}
})
}
}

func TestGetRequestURL(t *testing.T) {
t.Run("No Path", func(t *testing.T) {
req := httptest.NewRequest("GET", "/replace-me", nil)
req.URL.Path = ""
rec := httptest.NewRecorder()

reqURL := getRequestURL(rec, req)
if reqURL != nil {
t.Errorf("Expected nil response, got %v", reqURL)
}

resp := rec.Result()

if resp.StatusCode != http.StatusPreconditionFailed {
t.Errorf("Incorrect HTTP response code.\nExpected: %d\n Actual: %d\n", http.StatusPreconditionFailed, resp.StatusCode)
}

body, _ := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()

expectedBody := "{\"error\":{\"Code\":412,\"Message\":\"URL not provided\",\"Detail\":{\"method\":\"GET\",\"requestedURL\":\"\"}}}\n"
if string(body) != expectedBody {
t.Errorf("Incorrect response body.\nExpected: %q\n Actual: %q\n", expectedBody, body)
}
})

t.Run("Root Path", func(t *testing.T) {
req := httptest.NewRequest("POST", "/", nil)
rec := httptest.NewRecorder()

reqURL := getRequestURL(rec, req)
if reqURL != nil {
t.Errorf("Expected nil response, got %v", reqURL)
}

resp := rec.Result()

if resp.StatusCode != http.StatusPreconditionFailed {
t.Errorf("Incorrect HTTP response code.\nExpected: %d\n Actual: %d\n", http.StatusPreconditionFailed, resp.StatusCode)
}

body, _ := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()

expectedBody := "{\"error\":{\"Code\":412,\"Message\":\"URL not provided\",\"Detail\":{\"method\":\"POST\",\"requestedURL\":\"/\"}}}\n"
if string(body) != expectedBody {
t.Errorf("Incorrect response body.\nExpected: %q\n Actual: %q\n", expectedBody, body)
}
})

t.Run("Invalid", func(t *testing.T) {
req := httptest.NewRequest("GET", "/replace-me", nil)
req.URL.Path = string([]byte{'/', 0x7f})
rec := httptest.NewRecorder()

reqURL := getRequestURL(rec, req)
if reqURL != nil {
t.Errorf("Expected nil response, got %v", reqURL)
}

resp := rec.Result()

if resp.StatusCode != http.StatusPreconditionFailed {
t.Errorf("Incorrect HTTP response code.\nExpected: %d\n Actual: %d\n", http.StatusPreconditionFailed, resp.StatusCode)
}

body, _ := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()

expectedBody := "{\"error\":{\"Code\":412,\"Message\":\"parse http://\u007f: net/url: invalid control character in URL\",\"Detail\":{\"method\":\"GET\",\"requestedURL\":\"http://\u007f\"}}}\n"
if string(body) != expectedBody {
t.Errorf("Incorrect response body.\nExpected: %q\n Actual: %q\n", expectedBody, body)
}
})

t.Run("Unprefixed", func(t *testing.T) {
req := httptest.NewRequest("GET", "/hostname", nil)
rec := httptest.NewRecorder()

reqURL := getRequestURL(rec, req)
if reqURL == nil {
t.Fatal("Unexpected nil request URL.")
}

if reqURL.Scheme != "http" {
t.Errorf("Incorrect URL scheme.\nExpected: %q\n Actual: %q\n", "http", reqURL.Scheme)
}

if reqURL.Host != "hostname" {
t.Errorf("Incorrect URL host.\nExpected: %q\n Actual: %q\n", "hostname", reqURL.Host)
}
})

t.Run("Prefixed", func(t *testing.T) {
req := httptest.NewRequest("GET", "/https://hostname", nil)
rec := httptest.NewRecorder()

reqURL := getRequestURL(rec, req)
if reqURL == nil {
resp := rec.Result()
body, _ := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
t.Error(string(body))
t.Fatal("Unexpected nil request URL.")
}

if reqURL.Scheme != "https" {
t.Errorf("Incorrect URL scheme.\nExpected: %q\n Actual: %q\n", "https", reqURL.Scheme)
}

if reqURL.Host != "hostname" {
t.Errorf("Incorrect URL host.\nExpected: %q\n Actual: %q\n", "hostname", reqURL.Host)
}
})
}