| 123456789101112131415161718192021222324252627282930 |
- package httpClient
- import (
- "context"
- "errors"
- "net/http"
- "testing"
- "time"
- )
- func TestRequestContextStopsCanceledCall(t *testing.T) {
- client := &HttpClient{}
- for _, method := range []string{http.MethodGet, http.MethodPost} {
- t.Run(method, func(t *testing.T) {
- ctx, cancel := context.WithCancel(context.Background())
- cancel()
- seconds := time.Duration(5)
- options := &HttpClientOptions{Context: ctx, Timeout: &seconds}
- var err error
- if method == http.MethodGet {
- _, err = client.HttpGet("http://example.invalid", nil, options)
- } else {
- _, err = client.HttpPost("http://example.invalid", "body", options)
- }
- if !errors.Is(err, context.Canceled) {
- t.Fatalf("request error = %v, want context canceled", err)
- }
- })
- }
- }
|