github.com/grahambrereton-form3/tilt@v0.10.18/internal/testutils/httptest/http_client.go (about)

     1  package httptest
     2  
     3  import (
     4  	"io/ioutil"
     5  	"net/http"
     6  	"strings"
     7  	"sync"
     8  )
     9  
    10  type FakeClient struct {
    11  	Requests []http.Request
    12  	Response http.Response
    13  	Err      error
    14  
    15  	mu sync.Mutex
    16  }
    17  
    18  func (fc *FakeClient) Do(req *http.Request) (*http.Response, error) {
    19  	fc.mu.Lock()
    20  	defer fc.mu.Unlock()
    21  
    22  	fc.Requests = append(fc.Requests, *req)
    23  	r := fc.Response
    24  
    25  	return &r, fc.Err
    26  }
    27  
    28  func (fc *FakeClient) SetResponse(s string) {
    29  	fc.Response = http.Response{
    30  		StatusCode: http.StatusOK,
    31  		Body:       ioutil.NopCloser(strings.NewReader(s)),
    32  	}
    33  }
    34  
    35  func (fc *FakeClient) RequestURLs() []string {
    36  	var ret []string
    37  	for _, req := range fc.Requests {
    38  		ret = append(ret, req.URL.String())
    39  	}
    40  	return ret
    41  }
    42  
    43  func NewFakeClient() *FakeClient {
    44  	return &FakeClient{
    45  		Response: http.Response{
    46  			StatusCode: http.StatusInternalServerError,
    47  			Body:       ioutil.NopCloser(strings.NewReader("FakeClient response uninitialized")),
    48  		},
    49  	}
    50  }