Closed
Description
What version of Go are you using (go version
)?
$ go version: 1.17 (master/main branch)
Does this issue reproduce with the latest release?
Yes.
What did you do?
package testpackage
// Run this using $ go test
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"testing"
)
// RoundTripFuncError describes an interface type that we will then implement.
type RoundTripFuncError func(req *http.Request) *http.Response
// RoundTripError implements Golang's roundtrip interface. In this
// version we want to simulate an error when trying to connect to a
// given SPARQL server.
func (fn RoundTripFuncError) RoundTrip(request *http.Request) (*http.Response, error) {
return fn(request), fmt.Errorf("Mock error...")
}
// NewTestClientError returns *http.Client with Transport replaced to
// avoid making real calls out to the Internet. Transport will then do
// whatever we request of it. With this version we mock an error in the
// call.
func NewTestClientError(fn RoundTripFuncError) *http.Client {
return &http.Client{
Transport: RoundTripFuncError(fn),
}
}
// TestRoundtrip...
func TestRoundtrip(t *testing.T) {
httpClient := NewTestClientError(func(req *http.Request) *http.Response {
return &http.Response{
StatusCode: 418,
// Send response to be tested
Body: ioutil.NopCloser(bytes.NewBufferString("Some response")),
// Must be set to non-nil value or it panics
Header: make(http.Header),
}
})
req, err := http.NewRequest("GET", "http://example.com", nil)
if err != nil {
// Do something...
}
_, err = httpClient.Do(req)
if err != nil {
// Do something...
}
}
What did you expect to see?
Expecting an response or error to handle, or something else? (This looks like a developer's note to self, not necessarily something for everyone?)
What did you see instead?
Seeing an output to stderr
which I don't think we are expected to work with. It seems like this would be more idiomatic for the caller to deal with this somewhere, and not have the package itself output anything. I'd be interested to hear more about the intentions of the authorship though.