github.com/avenga/couper@v1.12.2/server/writer/response_test.go (about)

     1  package writer_test
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"testing"
     9  
    10  	"github.com/avenga/couper/internal/test"
    11  	"github.com/avenga/couper/server/writer"
    12  )
    13  
    14  func TestResponse_ChunkWrite(t *testing.T) {
    15  	helper := test.New(t)
    16  
    17  	rec := httptest.NewRecorder()
    18  	w := writer.NewResponseWriter(rec, "")
    19  
    20  	content := []byte("HTTP/1.1 404 Not Found\r\n\r\nBody")
    21  	for i := 0; i < len(content); i++ {
    22  		_, err := w.Write([]byte{content[i]})
    23  		helper.Must(err)
    24  	}
    25  
    26  	res := rec.Result()
    27  	if res.StatusCode != http.StatusNotFound || w.StatusCode() != http.StatusNotFound {
    28  		t.Errorf("Want: %d, got: %d", http.StatusNotFound, res.StatusCode)
    29  	}
    30  
    31  	b, err := io.ReadAll(res.Body)
    32  	helper.Must(err)
    33  
    34  	if !bytes.Equal(b, []byte("Body")) {
    35  		t.Errorf("Expected body content, got: %q", string(b))
    36  	}
    37  
    38  	if w.WrittenBytes() != 4 {
    39  		t.Errorf("Expected 4 written bytes, got: %d", w.WrittenBytes())
    40  	}
    41  }
    42  
    43  func TestResponse_ProtoWrite(t *testing.T) {
    44  	helper := test.New(t)
    45  
    46  	rec := httptest.NewRecorder()
    47  	w := writer.NewResponseWriter(rec, "")
    48  
    49  	response := &http.Response{
    50  		StatusCode: http.StatusOK,
    51  		ProtoMajor: 1,
    52  		ProtoMinor: 1,
    53  		Header: http.Header{
    54  			"Test": []string{"Value"},
    55  		},
    56  		Body:          io.NopCloser(bytes.NewBufferString("testContent")),
    57  		ContentLength: 11,
    58  	}
    59  
    60  	helper.Must(response.Write(w))
    61  
    62  	res := rec.Result()
    63  	if res.StatusCode != http.StatusOK || w.StatusCode() != http.StatusOK {
    64  		t.Errorf("Want: %d, got: %d", http.StatusOK, res.StatusCode)
    65  	}
    66  
    67  	if res.Header.Get("Content-Length") != "11" {
    68  		t.Error("Expected Content-Length header")
    69  	}
    70  
    71  	if res.Header.Get("Test") != "Value" {
    72  		t.Errorf("Expected Test header, got: %q", res.Header.Get("Test"))
    73  	}
    74  
    75  	b, err := io.ReadAll(res.Body)
    76  	helper.Must(err)
    77  
    78  	if !bytes.Equal(b, []byte("testContent")) {
    79  		t.Errorf("Expected body content, got: %q", string(b))
    80  	}
    81  
    82  	if w.WrittenBytes() != 11 {
    83  		t.Errorf("Expected 11 written bytes, got: %d", w.WrittenBytes())
    84  	}
    85  }
    86  
    87  func TestResponse_ProtoWriteAll(t *testing.T) {
    88  	helper := test.New(t)
    89  
    90  	rec := httptest.NewRecorder()
    91  	w := writer.NewResponseWriter(rec, "")
    92  
    93  	response := &http.Response{
    94  		StatusCode: http.StatusForbidden,
    95  		ProtoMajor: 1,
    96  		ProtoMinor: 1,
    97  		Header: http.Header{
    98  			"Test": []string{"Value"},
    99  		},
   100  	}
   101  
   102  	buf := &bytes.Buffer{}
   103  	helper.Must(response.Write(buf))
   104  
   105  	_, err := buf.WriteTo(w)
   106  	helper.Must(err)
   107  
   108  	res := rec.Result()
   109  	if res.StatusCode != http.StatusForbidden || w.StatusCode() != http.StatusForbidden {
   110  		t.Errorf("Want: %d, got: %d", http.StatusOK, res.StatusCode)
   111  	}
   112  
   113  	if res.Header.Get("Test") != "Value" {
   114  		t.Errorf("Expected Test header, got: %q", res.Header.Get("Test"))
   115  	}
   116  }