github.com/drone/runner-go@v1.12.0/logger/dumper_test.go (about)

     1  // Copyright 2019 Drone.IO Inc. All rights reserved.
     2  // Use of this source code is governed by the Polyform License
     3  // that can be found in the LICENSE file.
     4  
     5  package logger
     6  
     7  import (
     8  	"bytes"
     9  	"net/http"
    10  	"os"
    11  	"testing"
    12  )
    13  
    14  func TestStandardDumper(t *testing.T) {
    15  	d := StandardDumper(true)
    16  	if s, ok := d.(*standardDumper); !ok {
    17  		t.Errorf("Expect standard dumper")
    18  	} else if s.out != os.Stdout {
    19  		t.Errorf("Expect standard dumper set to stdout")
    20  	}
    21  }
    22  
    23  func TestDiscardDumper(t *testing.T) {
    24  	d := DiscardDumper()
    25  	if _, ok := d.(*discardDumper); !ok {
    26  		t.Errorf("Expect discard dumper")
    27  	}
    28  }
    29  
    30  func TestStandardDumper_DumpRequest(t *testing.T) {
    31  	buf := new(bytes.Buffer)
    32  	r, _ := http.NewRequest("GET", "http://example.com", nil)
    33  	d := StandardDumper(true).(*standardDumper)
    34  	d.out = buf
    35  	d.DumpRequest(r)
    36  
    37  	want := "GET / HTTP/1.1\r\nHost: example.com\r\nUser-Agent: Go-http-client/1.1\r\nAccept-Encoding: gzip\r\n\r\n"
    38  	got := buf.String()
    39  	if got != want {
    40  		t.Errorf("Got dumped request %q", got)
    41  	}
    42  }
    43  
    44  func TestStandardDumper_DumpResponse(t *testing.T) {
    45  	buf := new(bytes.Buffer)
    46  	r := &http.Response{
    47  		Status:     "200 OK",
    48  		StatusCode: 200,
    49  		Proto:      "HTTP/1.0",
    50  		ProtoMajor: 1,
    51  		ProtoMinor: 0,
    52  	}
    53  	d := StandardDumper(true).(*standardDumper)
    54  	d.out = buf
    55  	d.DumpResponse(r)
    56  
    57  	want := "HTTP/1.0 200 OK\r\nContent-Length: 0\r\n\r\n"
    58  	got := buf.String()
    59  	if got != want {
    60  		t.Errorf("Got dumped request %q", got)
    61  	}
    62  }