github.com/cloudwego/hertz@v0.9.3/pkg/common/ut/response_test.go (about)

     1  /*
     2   * Copyright 2022 CloudWeGo Authors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package ut
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/cloudwego/hertz/pkg/common/test/assert"
    23  	"github.com/cloudwego/hertz/pkg/protocol/consts"
    24  )
    25  
    26  func TestResult(t *testing.T) {
    27  	r := new(ResponseRecorder)
    28  	ret := r.Result()
    29  	assert.DeepEqual(t, consts.StatusOK, ret.StatusCode())
    30  }
    31  
    32  func TestFlush(t *testing.T) {
    33  	r := new(ResponseRecorder)
    34  	r.Flush()
    35  	ret := r.Result()
    36  	assert.DeepEqual(t, consts.StatusOK, ret.StatusCode())
    37  }
    38  
    39  func TestWriterHeader(t *testing.T) {
    40  	r := NewRecorder()
    41  	r.WriteHeader(consts.StatusCreated)
    42  	r.WriteHeader(consts.StatusOK)
    43  	ret := r.Result()
    44  	assert.DeepEqual(t, consts.StatusCreated, ret.StatusCode())
    45  }
    46  
    47  func TestWriteString(t *testing.T) {
    48  	r := NewRecorder()
    49  	r.WriteString("hello")
    50  	ret := r.Result()
    51  	assert.DeepEqual(t, "hello", string(ret.Body()))
    52  }
    53  
    54  func TestWrite(t *testing.T) {
    55  	r := NewRecorder()
    56  	r.Write([]byte("hello"))
    57  	ret := r.Result()
    58  	assert.DeepEqual(t, "hello", string(ret.Body()))
    59  }