go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/web/raw_result_test.go (about) 1 /* 2 3 Copyright (c) 2023 - Present. Will Charczuk. All rights reserved. 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository. 5 6 */ 7 8 package web 9 10 import ( 11 "bytes" 12 "fmt" 13 "net/http" 14 "testing" 15 16 "go.charczuk.com/sdk/assert" 17 ) 18 19 func Test_Raw(t *testing.T) { 20 r := Raw([]byte("foo")) 21 assert.ItsEqual(t, http.StatusOK, r.StatusCode) 22 assert.ItsEqual(t, ContentTypeText, r.ContentType) 23 assert.ItsEqual(t, "foo", string(r.Response)) 24 } 25 26 func Test_RawString(t *testing.T) { 27 r := RawString("foo") 28 assert.ItsEqual(t, http.StatusOK, r.StatusCode) 29 assert.ItsEqual(t, ContentTypeText, r.ContentType) 30 assert.ItsEqual(t, "foo", string(r.Response)) 31 } 32 33 func Test_RawWithContentType(t *testing.T) { 34 r := RawWithContentType("text/plain", []byte("foo")) 35 assert.ItsEqual(t, http.StatusOK, r.StatusCode) 36 assert.ItsEqual(t, "text/plain", r.ContentType) 37 assert.ItsEqual(t, "foo", string(r.Response)) 38 } 39 40 func Test_RawWithErr(t *testing.T) { 41 r := RawWithErr("text/plain", []byte("foo"), fmt.Errorf("this is only a test")) 42 assert.ItsEqual(t, http.StatusOK, r.StatusCode) 43 assert.ItsEqual(t, "text/plain", r.ContentType) 44 assert.ItsEqual(t, "foo", string(r.Response)) 45 assert.ItsEqual(t, "this is only a test", r.Err.Error()) 46 } 47 48 func Test_Raw_Render(t *testing.T) { 49 b := new(bytes.Buffer) 50 res := NewMockResponse(b) 51 err := (&RawResult{ 52 StatusCode: http.StatusGone, 53 ContentType: ContentTypeApplicationOctetStream, 54 Response: []byte("hello"), 55 Err: fmt.Errorf("this is only a test"), 56 }).Render(newContext(res, nil, nil, nil)) 57 58 assert.ItsNotNil(t, err) 59 assert.ItsEqual(t, "this is only a test", err.Error()) 60 assert.ItsEqual(t, "hello", b.String()) 61 }