github.com/seeker-insurance/kit@v0.0.13/web/handlers_test.go (about)

     1  package web
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/labstack/echo"
     9  	"github.com/seeker-insurance/kit/jsonapi"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestHealthz(t *testing.T) {
    14  	t.Run("panic!", func(t *testing.T) {
    15  		defer func() {
    16  			recover()
    17  		}()
    18  		ctx := newMock(keyVal("oops", "1"), nil)
    19  		Healthz(ctx)
    20  		t.Error("should have panicked")
    21  	})
    22  
    23  	var want error = &jsonapi.ErrorObject{
    24  		Title:  "Api Error",
    25  		Status: fmt.Sprintf("%d", http.StatusBadRequest),
    26  		Detail: "missing parameters: foo,bar",
    27  	}
    28  	got := Healthz(newMock(keyVal("apiErr", "1"), nil))
    29  	assert.Equal(t, want, got)
    30  
    31  	want = echo.NewHTTPError(http.StatusInternalServerError)
    32  	assert.Equal(t, want, Healthz(newMock(keyVal("500", "1"), nil)))
    33  
    34  	want = echo.NewHTTPError(http.StatusBadRequest, "Missing value.")
    35  	assert.Equal(t, want, Healthz(newMock(keyVal("400", "1"), nil)))
    36  
    37  	var ctx ApiContext = stringContext{newMock(nil, nil)}
    38  	want = fmt.Errorf("%d:%s", http.StatusOK, "live")
    39  	assert.Equal(t, want, Healthz(ctx))
    40  
    41  }
    42  
    43  type stringContext struct {
    44  	ApiContext
    45  }
    46  
    47  func (ctx stringContext) String(code int, s string) error {
    48  	return fmt.Errorf("%d:%s", code, s)
    49  }