From de9f8bc22e3cf43283d924761810c0cee1449528 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sun, 2 Jul 2017 17:17:39 +0100 Subject: [PATCH] Wrong status code but correct content should error --- vape.go | 12 ++++++--- vape_test.go | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 4 deletions(-) diff --git a/vape.go b/vape.go index de6e94c..c9f5537 100644 --- a/vape.go +++ b/vape.go @@ -23,11 +23,15 @@ type SmokeTestResult struct { // Passed determines if the SmokeTest passed successfully func (result SmokeTestResult) Passed() bool { - if result.Test.Content != "" { - return !result.contentMatched() + if !result.statusCodeMatched() { + return false } - return result.statusCodeMatched() + if result.Test.Content != "" && !result.contentMatched() { + return false + } + + return true } func (result SmokeTestResult) statusCodeMatched() bool { @@ -35,7 +39,7 @@ func (result SmokeTestResult) statusCodeMatched() bool { } func (result SmokeTestResult) contentMatched() bool { - return (strings.Contains(string(result.ActualContent), result.Test.Content) == false) + return (strings.Contains(string(result.ActualContent), result.Test.Content) == true) } // SmokeTests is a slice of smoke tests to perform. diff --git a/vape_test.go b/vape_test.go index f97d6b9..ef63dbc 100644 --- a/vape_test.go +++ b/vape_test.go @@ -24,6 +24,79 @@ var test = SmokeTest{ ExpectedStatusCode: 200, } +func TestSmokeTestResult(t *testing.T) { + t.Run("PassedWhenStatusCodeMatches", func(t *testing.T) { + result := SmokeTestResult{ + Test: SmokeTest{ + ExpectedStatusCode: 200, + }, + ActualStatusCode: 200, + } + + if result.Passed() != true { + t.Errorf("expected pass: true, got: %v", result.Passed()) + } + }) + + t.Run("FailedWhenStatusCodeDifferent", func(t *testing.T) { + result := SmokeTestResult{ + Test: SmokeTest{ + ExpectedStatusCode: 200, + }, + ActualStatusCode: 404, + } + + if result.Passed() != false { + t.Errorf("expected pass: false, got: %v", result.Passed()) + } + }) + + t.Run("PassedWhenStatusCodeAndContentMatches", func(t *testing.T) { + result := SmokeTestResult{ + Test: SmokeTest{ + ExpectedStatusCode: 200, + Content: "Hello", + }, + ActualStatusCode: 200, + ActualContent: []byte("Hello"), + } + + if result.Passed() != true { + t.Errorf("expected pass: true, got: %v", result.Passed()) + } + }) + + t.Run("FailedWhenStatusCodeMatchesButContentDoesnt", func(t *testing.T) { + result := SmokeTestResult{ + Test: SmokeTest{ + ExpectedStatusCode: 200, + Content: "Hello", + }, + ActualStatusCode: 200, + ActualContent: []byte("Goodbye"), + } + + if result.Passed() != false { + t.Errorf("expected pass: false, got: %v", result.Passed()) + } + }) + + t.Run("FailedWhenStatusCodeDifferentButContentMatches", func(t *testing.T) { + result := SmokeTestResult{ + Test: SmokeTest{ + ExpectedStatusCode: 200, + Content: "Hello", + }, + ActualStatusCode: 404, + ActualContent: []byte("Hello"), + } + + if result.Passed() != false { + t.Errorf("expected pass: false, got: %v", result.Passed()) + } + }) +} + func TestProcess(t *testing.T) { resCh, errCh := make(chan SmokeTestResult, 1), make(chan error, 1) baseURL, err := url.Parse("http://base.url")