Skip to content

Commit

Permalink
replace st with expect
Browse files Browse the repository at this point in the history
  • Loading branch information
Clivern committed Feb 4, 2020
1 parent c5fd19a commit 93f3aa0
Show file tree
Hide file tree
Showing 7 changed files with 287 additions and 71 deletions.
8 changes: 2 additions & 6 deletions beetle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"testing"

"github.com/clivern/beetle/internal/app/module"
"github.com/clivern/beetle/pkg"

"github.com/drone/envsubst"
"github.com/spf13/viper"
Expand Down Expand Up @@ -44,11 +45,6 @@ func TestMain(t *testing.T) {
viper.SetConfigType("yaml")
viper.ReadConfig(bytes.NewBuffer([]byte(configParsed)))

got := viper.GetString("app.mode")
want := "test"

if got != want {
t.Errorf("got %v, want %v", got, want)
}
pkg.Expect(t, viper.GetString("app.mode"), "test")
})
}
28 changes: 8 additions & 20 deletions internal/app/controller/health_check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"testing"

"github.com/clivern/beetle/internal/app/module"
"github.com/clivern/beetle/pkg"

"github.com/drone/envsubst"
"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -51,6 +52,10 @@ func TestHealthCheck(t *testing.T) {

// TestHealthCheckController
t.Run("TestHealthCheckController", func(t *testing.T) {
gin.SetMode(gin.ReleaseMode)
gin.DefaultWriter = ioutil.Discard
gin.DisableConsoleColor()

router := gin.Default()

router.GET("/_healthcheck", HealthCheck)
Expand All @@ -59,25 +64,8 @@ func TestHealthCheck(t *testing.T) {
req, _ := http.NewRequest("GET", "/_healthcheck", nil)
router.ServeHTTP(w, req)

got := viper.GetString("app.mode")
want := "test"

if got != want {
t.Errorf("got %v, want %v", got, want)
}

got1 := w.Code
want1 := 200

if got1 != want1 {
t.Errorf("got %v, want %v", got1, want1)
}

got2 := strings.TrimSpace(w.Body.String())
want2 := `{"status":"ok"}`

if got2 != want2 {
t.Errorf("got %v, want %v", got2, want2)
}
pkg.Expect(t, viper.GetString("app.mode"), "test")
pkg.Expect(t, w.Code, 200)
pkg.Expect(t, strings.TrimSpace(w.Body.String()), `{"status":"ok"}`)
})
}
17 changes: 13 additions & 4 deletions internal/app/module/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package module

import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net/http"
Expand All @@ -22,7 +23,7 @@ func NewHTTPClient() *HTTPClient {
}

// Get http call
func (h *HTTPClient) Get(endpoint string, parameters map[string]string, headers map[string]string) (*http.Response, error) {
func (h *HTTPClient) Get(ctx context.Context, endpoint string, parameters map[string]string, headers map[string]string) (*http.Response, error) {

endpoint, err := h.BuildParameters(endpoint, parameters)

Expand All @@ -32,6 +33,8 @@ func (h *HTTPClient) Get(endpoint string, parameters map[string]string, headers

req, _ := http.NewRequest("GET", endpoint, nil)

req = req.WithContext(ctx)

for k, v := range headers {
req.Header.Add(k, v)
}
Expand All @@ -48,7 +51,7 @@ func (h *HTTPClient) Get(endpoint string, parameters map[string]string, headers
}

// Post http call
func (h *HTTPClient) Post(endpoint string, data string, parameters map[string]string, headers map[string]string) (*http.Response, error) {
func (h *HTTPClient) Post(ctx context.Context, endpoint string, data string, parameters map[string]string, headers map[string]string) (*http.Response, error) {

endpoint, err := h.BuildParameters(endpoint, parameters)

Expand All @@ -58,6 +61,8 @@ func (h *HTTPClient) Post(endpoint string, data string, parameters map[string]st

req, _ := http.NewRequest("POST", endpoint, bytes.NewBuffer([]byte(data)))

req = req.WithContext(ctx)

for k, v := range headers {
req.Header.Add(k, v)
}
Expand All @@ -74,7 +79,7 @@ func (h *HTTPClient) Post(endpoint string, data string, parameters map[string]st
}

// Put http call
func (h *HTTPClient) Put(endpoint string, data string, parameters map[string]string, headers map[string]string) (*http.Response, error) {
func (h *HTTPClient) Put(ctx context.Context, endpoint string, data string, parameters map[string]string, headers map[string]string) (*http.Response, error) {

endpoint, err := h.BuildParameters(endpoint, parameters)

Expand All @@ -84,6 +89,8 @@ func (h *HTTPClient) Put(endpoint string, data string, parameters map[string]str

req, _ := http.NewRequest("PUT", endpoint, bytes.NewBuffer([]byte(data)))

req = req.WithContext(ctx)

for k, v := range headers {
req.Header.Add(k, v)
}
Expand All @@ -100,7 +107,7 @@ func (h *HTTPClient) Put(endpoint string, data string, parameters map[string]str
}

// Delete http call
func (h *HTTPClient) Delete(endpoint string, parameters map[string]string, headers map[string]string) (*http.Response, error) {
func (h *HTTPClient) Delete(ctx context.Context, endpoint string, parameters map[string]string, headers map[string]string) (*http.Response, error) {

endpoint, err := h.BuildParameters(endpoint, parameters)

Expand All @@ -110,6 +117,8 @@ func (h *HTTPClient) Delete(endpoint string, parameters map[string]string, heade

req, _ := http.NewRequest("DELETE", endpoint, nil)

req = req.WithContext(ctx)

for k, v := range headers {
req.Header.Add(k, v)
}
Expand Down
228 changes: 228 additions & 0 deletions internal/app/module/http_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
// Copyright 2020 Clivern. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.

package module

import (
"context"
"net/http"
"strings"
"testing"

"github.com/clivern/beetle/pkg"
)

// TestHttpGet test cases
func TestHttpGet(t *testing.T) {
t.Run("TestHttpGet", func(t *testing.T) {
httpClient := NewHTTPClient()
response, error := httpClient.Get(
context.Background(),
"https://httpbin.org/get",
map[string]string{"arg1": "value1"},
map[string]string{"X-Auth": "hipp-123"},
)

pkg.Expect(t, http.StatusOK, httpClient.GetStatusCode(response))
pkg.Expect(t, nil, error)

body, error := httpClient.ToString(response)

pkg.Expect(t, true, strings.Contains(body, "value1"))
pkg.Expect(t, true, strings.Contains(body, "arg1"))
pkg.Expect(t, true, strings.Contains(body, "arg1=value1"))
pkg.Expect(t, true, strings.Contains(body, "X-Auth"))
pkg.Expect(t, true, strings.Contains(body, "hipp-123"))
pkg.Expect(t, nil, error)
})
}

// TestHttpDelete test cases
func TestHttpDelete(t *testing.T) {
t.Run("TestHttpDelete", func(t *testing.T) {
httpClient := NewHTTPClient()
response, error := httpClient.Delete(
context.Background(),
"https://httpbin.org/delete",
map[string]string{"arg1": "value1"},
map[string]string{"X-Auth": "hipp-123"},
)

pkg.Expect(t, http.StatusOK, httpClient.GetStatusCode(response))
pkg.Expect(t, nil, error)

body, error := httpClient.ToString(response)

pkg.Expect(t, true, strings.Contains(body, "value1"))
pkg.Expect(t, true, strings.Contains(body, "arg1"))
pkg.Expect(t, true, strings.Contains(body, "arg1=value1"))
pkg.Expect(t, true, strings.Contains(body, "X-Auth"))
pkg.Expect(t, true, strings.Contains(body, "hipp-123"))
pkg.Expect(t, nil, error)
})
}

// TestHttpPost test cases
func TestHttpPost(t *testing.T) {
t.Run("TestHttpPost", func(t *testing.T) {
httpClient := NewHTTPClient()
response, error := httpClient.Post(
context.Background(),
"https://httpbin.org/post",
`{"Username":"admin", "Password":"12345"}`,
map[string]string{"arg1": "value1"},
map[string]string{"X-Auth": "hipp-123"},
)

pkg.Expect(t, http.StatusOK, httpClient.GetStatusCode(response))
pkg.Expect(t, nil, error)

body, error := httpClient.ToString(response)

pkg.Expect(t, true, strings.Contains(body, `"12345"`))
pkg.Expect(t, true, strings.Contains(body, `"Username"`))
pkg.Expect(t, true, strings.Contains(body, `"admin"`))
pkg.Expect(t, true, strings.Contains(body, `"Password"`))
pkg.Expect(t, true, strings.Contains(body, "value1"))
pkg.Expect(t, true, strings.Contains(body, "arg1"))
pkg.Expect(t, true, strings.Contains(body, "arg1=value1"))
pkg.Expect(t, true, strings.Contains(body, "X-Auth"))
pkg.Expect(t, true, strings.Contains(body, "hipp-123"))
pkg.Expect(t, nil, error)
})
}

// TestHttpPut test cases
func TestHttpPut(t *testing.T) {
t.Run("TestHttpPut", func(t *testing.T) {
httpClient := NewHTTPClient()
response, error := httpClient.Put(
context.Background(),
"https://httpbin.org/put",
`{"Username":"admin", "Password":"12345"}`,
map[string]string{"arg1": "value1"},
map[string]string{"X-Auth": "hipp-123"},
)

pkg.Expect(t, http.StatusOK, httpClient.GetStatusCode(response))
pkg.Expect(t, nil, error)

body, error := httpClient.ToString(response)

pkg.Expect(t, true, strings.Contains(body, `"12345"`))
pkg.Expect(t, true, strings.Contains(body, `"Username"`))
pkg.Expect(t, true, strings.Contains(body, `"admin"`))
pkg.Expect(t, true, strings.Contains(body, `"Password"`))
pkg.Expect(t, true, strings.Contains(body, "value1"))
pkg.Expect(t, true, strings.Contains(body, "arg1"))
pkg.Expect(t, true, strings.Contains(body, "arg1=value1"))
pkg.Expect(t, true, strings.Contains(body, "X-Auth"))
pkg.Expect(t, true, strings.Contains(body, "hipp-123"))
pkg.Expect(t, nil, error)
})
}

// TestHttpGetStatusCode1 test cases
func TestHttpGetStatusCode1(t *testing.T) {
t.Run("TestHttpGetStatusCode1", func(t *testing.T) {
httpClient := NewHTTPClient()
response, error := httpClient.Get(
context.Background(),
"https://httpbin.org/status/200",
map[string]string{"arg1": "value1"},
map[string]string{"X-Auth": "hipp-123"},
)

pkg.Expect(t, http.StatusOK, httpClient.GetStatusCode(response))
pkg.Expect(t, nil, error)

body, error := httpClient.ToString(response)

pkg.Expect(t, "", body)
pkg.Expect(t, nil, error)
})
}

// TestHttpGetStatusCode2 test cases
func TestHttpGetStatusCode2(t *testing.T) {
t.Run("TestHttpGetStatusCode2", func(t *testing.T) {
httpClient := NewHTTPClient()
response, error := httpClient.Get(
context.Background(),
"https://httpbin.org/status/500",
map[string]string{"arg1": "value1"},
map[string]string{"X-Auth": "hipp-123"},
)

pkg.Expect(t, http.StatusInternalServerError, httpClient.GetStatusCode(response))
pkg.Expect(t, nil, error)

body, error := httpClient.ToString(response)

pkg.Expect(t, "", body)
pkg.Expect(t, nil, error)
})
}

// TestHttpGetStatusCode3 test cases
func TestHttpGetStatusCode3(t *testing.T) {
t.Run("TestHttpGetStatusCode3", func(t *testing.T) {
httpClient := NewHTTPClient()
response, error := httpClient.Get(
context.Background(),
"https://httpbin.org/status/404",
map[string]string{"arg1": "value1"},
map[string]string{"X-Auth": "hipp-123"},
)

pkg.Expect(t, http.StatusNotFound, httpClient.GetStatusCode(response))
pkg.Expect(t, nil, error)

body, error := httpClient.ToString(response)

pkg.Expect(t, "", body)
pkg.Expect(t, nil, error)
})
}

// TestHttpGetStatusCode4 test cases
func TestHttpGetStatusCode4(t *testing.T) {
t.Run("TestHttpGetStatusCode4", func(t *testing.T) {
httpClient := NewHTTPClient()
response, error := httpClient.Get(
context.Background(),
"https://httpbin.org/status/201",
map[string]string{"arg1": "value1"},
map[string]string{"X-Auth": "hipp-123"},
)

pkg.Expect(t, http.StatusCreated, httpClient.GetStatusCode(response))
pkg.Expect(t, nil, error)

body, error := httpClient.ToString(response)

pkg.Expect(t, "", body)
pkg.Expect(t, nil, error)
})
}

// TestBuildParameters test cases
func TestBuildParameters(t *testing.T) {
t.Run("TestBuildParameters", func(t *testing.T) {
httpClient := NewHTTPClient()
url, error := httpClient.BuildParameters("http://127.0.0.1", map[string]string{"arg1": "value1"})

pkg.Expect(t, "http://127.0.0.1?arg1=value1", url)
pkg.Expect(t, nil, error)
})
}

// TestBuildData test cases
func TestBuildData(t *testing.T) {
t.Run("TestBuildData", func(t *testing.T) {
httpClient := NewHTTPClient()
pkg.Expect(t, httpClient.BuildData(map[string]string{}), "")
pkg.Expect(t, httpClient.BuildData(map[string]string{"arg1": "value1"}), "arg1=value1")
})
}
10 changes: 10 additions & 0 deletions internal/app/util/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"path/filepath"
"reflect"
"strings"
"testing"

"github.com/satori/go.uuid"
)
Expand Down Expand Up @@ -86,3 +87,12 @@ func Unset(a []string, i int) []string {
a[len(a)-1] = ""
return a[:len(a)-1]
}

// Expect compare two values for testing
func Expect(t *testing.T, got, want interface{}) {
t.Logf(`Comparing values %v, %v`, got, want)

if !reflect.DeepEqual(got, want) {
t.Errorf(`got %v, want %v`, got, want)
}
}
Loading

0 comments on commit 93f3aa0

Please sign in to comment.