...

Source file src/github.com/go-kivik/kivik/v4/couchdb/chttp/mock_test.go

Documentation: github.com/go-kivik/kivik/v4/couchdb/chttp

     1  // Licensed under the Apache License, Version 2.0 (the "License"); you may not
     2  // use this file except in compliance with the License. You may obtain a copy of
     3  // the License at
     4  //
     5  //  http://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     9  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    10  // License for the specific language governing permissions and limitations under
    11  // the License.
    12  
    13  package chttp
    14  
    15  import (
    16  	"io"
    17  	"net/http"
    18  	"net/url"
    19  	"strings"
    20  )
    21  
    22  type customTransport func(*http.Request) (*http.Response, error)
    23  
    24  var _ http.RoundTripper = customTransport(nil)
    25  
    26  func (c customTransport) RoundTrip(req *http.Request) (*http.Response, error) {
    27  	return c(req)
    28  }
    29  
    30  func newCustomClient(dsn string, fn func(*http.Request) (*http.Response, error)) *Client {
    31  	c := &Client{
    32  		Client: &http.Client{
    33  			Transport: customTransport(fn),
    34  		},
    35  	}
    36  	var err error
    37  	if dsn == "" {
    38  		dsn = "http://example.com/"
    39  	}
    40  	c.dsn, err = url.Parse(dsn)
    41  	if err != nil {
    42  		panic(err)
    43  	}
    44  	c.basePath = strings.TrimSuffix(c.dsn.Path, "/")
    45  	return c
    46  }
    47  
    48  func newTestClient(resp *http.Response, err error) *Client {
    49  	return newCustomClient("", func(_ *http.Request) (*http.Response, error) {
    50  		return resp, err
    51  	})
    52  }
    53  
    54  func Body(str string) io.ReadCloser {
    55  	return io.NopCloser(strings.NewReader(str))
    56  }
    57  

View as plain text