github.com/blend/go-sdk@v1.20220411.3/web/json_result_provider_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package web
     9  
    10  import (
    11  	"fmt"
    12  	"net/http"
    13  	"testing"
    14  
    15  	"github.com/blend/go-sdk/assert"
    16  )
    17  
    18  func TestJSONResultProvider(t *testing.T) {
    19  	assert := assert.New(t)
    20  
    21  	notFound, ok := JSON.NotFound().(*JSONResult)
    22  	assert.True(ok)
    23  	assert.Equal(http.StatusNotFound, notFound.StatusCode)
    24  	assert.Equal("Not Found", notFound.Response)
    25  
    26  	notAuthorized, ok := JSON.NotAuthorized().(*JSONResult)
    27  	assert.True(ok)
    28  	assert.Equal(http.StatusUnauthorized, notAuthorized.StatusCode)
    29  	assert.Equal("Not Authorized", notAuthorized.Response)
    30  
    31  	forbidden, ok := JSON.Forbidden().(*JSONResult)
    32  	assert.True(ok)
    33  	assert.Equal(http.StatusForbidden, forbidden.StatusCode)
    34  	assert.Equal("Forbidden", forbidden.Response)
    35  
    36  	badRequest, ok := JSON.BadRequest(nil).(*JSONResult)
    37  	assert.True(ok)
    38  	assert.Equal(http.StatusBadRequest, badRequest.StatusCode)
    39  	assert.Equal("Bad Request", badRequest.Response)
    40  
    41  	badRequestErr, ok := JSON.BadRequest(fmt.Errorf("bad-request")).(*JSONResult)
    42  	assert.True(ok)
    43  	assert.Equal(http.StatusBadRequest, badRequestErr.StatusCode)
    44  	assert.Equal("bad-request", badRequestErr.Response)
    45  
    46  	okRes, ok := JSON.OK().(*JSONResult)
    47  	assert.True(ok)
    48  	assert.Equal(http.StatusOK, okRes.StatusCode)
    49  	assert.Equal("OK!", okRes.Response)
    50  
    51  	statusRes, ok := JSON.Status(http.StatusBadGateway, "test").(*JSONResult)
    52  	assert.True(ok)
    53  	assert.Equal(http.StatusBadGateway, statusRes.StatusCode)
    54  	assert.Equal("test", statusRes.Response)
    55  
    56  	res, ok := JSON.Result("foo").(*JSONResult)
    57  	assert.True(ok)
    58  	assert.Equal(http.StatusOK, res.StatusCode)
    59  	assert.Equal("foo", res.Response)
    60  
    61  	internalError := JSON.InternalError(fmt.Errorf("only a test"))
    62  
    63  	typed, ok := internalError.(*LoggedErrorResult)
    64  	assert.True(ok)
    65  	assert.Equal(fmt.Errorf("only a test"), typed.Error)
    66  	inner := typed.Result.(*JSONResult)
    67  	assert.Equal(http.StatusInternalServerError, inner.StatusCode)
    68  	assert.Equal("only a test", inner.Response)
    69  }