github.com/blend/go-sdk@v1.20220411.3/web/json_result_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  	"bytes"
    12  	"net/http"
    13  	"testing"
    14  
    15  	"github.com/blend/go-sdk/assert"
    16  	"github.com/blend/go-sdk/webutil"
    17  )
    18  
    19  func TestJSONResultRender(t *testing.T) {
    20  	assert := assert.New(t)
    21  
    22  	buf := new(bytes.Buffer)
    23  	w := webutil.NewMockResponse(buf)
    24  	r := NewCtx(w, webutil.NewMockRequest("GET", "/"))
    25  
    26  	jr := &JSONResult{
    27  		StatusCode: http.StatusOK,
    28  		Response: map[string]interface{}{
    29  			"foo": "bar",
    30  		},
    31  	}
    32  
    33  	assert.Nil(jr.Render(r))
    34  	assert.Equal(http.StatusOK, w.StatusCode())
    35  	assert.Equal("{\"foo\":\"bar\"}\n", buf.String())
    36  }
    37  
    38  func TestJSONResultRenderStatusCode(t *testing.T) {
    39  	assert := assert.New(t)
    40  
    41  	buf := new(bytes.Buffer)
    42  	w := webutil.NewMockResponse(buf)
    43  	r := NewCtx(w, webutil.NewMockRequest("GET", "/"))
    44  
    45  	jr := &JSONResult{
    46  		StatusCode: http.StatusBadRequest,
    47  		Response: map[string]interface{}{
    48  			"foo": "bar",
    49  		},
    50  	}
    51  
    52  	assert.Nil(jr.Render(r))
    53  	assert.Equal(http.StatusBadRequest, w.StatusCode())
    54  	assert.Equal("{\"foo\":\"bar\"}\n", buf.String())
    55  }