Skip to content

Instantly share code, notes, and snippets.

@podhmo
Created March 9, 2021 23:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save podhmo/042e7bbc0420406e9b8b32f0bb3f8da1 to your computer and use it in GitHub Desktop.
Save podhmo/042e7bbc0420406e9b8b32f0bb3f8da1 to your computer and use it in GitHub Desktop.
package expecterror
import (
"errors"
"fmt"
"net/http"
"testing"
"time"
)
type errorTransport struct {
T *testing.T
genErr func() error
}
func (t *errorTransport) RoundTrip(req *http.Request) (*http.Response, error) {
err := t.genErr()
t.T.Logf("error forcely %T", err)
return nil, err
}
func TestErrorTransport(t *testing.T) {
this := fmt.Errorf("*THIS*")
transport := &errorTransport{T: t, genErr: func() error { return this }}
client := &http.Client{Timeout: 100 * time.Millisecond, Transport: transport}
_, err := client.Get("http://example.net.dummy") // テキトーなURL
if err == nil {
t.Errorf("error is expected but nil")
}
if !errors.Is(err, this) {
t.Errorf("%#[2]v is expected but return error is %[1]T, %+[1]v", err, this)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment