| index f4a092b4d..a3e435e97 100644 |
| --- a/internal/console/legalfetch_test.go |
| +++ b/internal/console/legalfetch_test.go |
| @@ -4,8 +4,10 @@ import ( |
| "context" |
| "crypto/tls" |
| "crypto/x509" |
| + "errors" |
| "net/http" |
| "net/http/httptest" |
| + "net/url" |
| "strings" |
| "testing" |
| "time" |
| @@ -157,3 +159,46 @@ func TestCanonicalAgreementRefuses(t *testing.T) { |
| } |
| } |
| } |
| + |
| +// A fetch cancelled by its caller (the operator's request went away) is its |
| +// own category, not a connection failure. |
| +func TestFetchLegalTextCancelled(t *testing.T) { |
| + srv, _ := countingServer(t, func(w http.ResponseWriter, r *http.Request) { <-r.Context().Done() }) |
| + ctx, cancel := context.WithCancel(context.Background()) |
| + cancel() |
| + _, err := fetchLegalText(ctx, legalTestClient(srv), srv.URL) |
| + if err == nil || err.Error() != "cancelled" { |
| + t.Fatalf("err = %v, want cancelled", err) |
| + } |
| + |
| + // Cancelled mid-request, once the server has the request. |
| + arrived := make(chan struct{}) |
| + srv2, _ := countingServer(t, func(w http.ResponseWriter, r *http.Request) { |
| + close(arrived) |
| + <-r.Context().Done() |
| + }) |
| + ctx2, cancel2 := context.WithCancel(context.Background()) |
| + defer cancel2() |
| + go func() { <-arrived; cancel2() }() |
| + _, err = fetchLegalText(ctx2, legalTestClient(srv2), srv2.URL) |
| + if err == nil || err.Error() != "cancelled" { |
| + t.Fatalf("mid-request err = %v, want cancelled", err) |
| + } |
| +} |
| + |
| +// legalFetchErr's categories, for the errors a client returns. |
| +func TestFetchLegalErrCategories(t *testing.T) { |
| + for _, tc := range []struct { |
| + err error |
| + want string |
| + }{ |
| + {&url.Error{Op: "Get", URL: "https://x", Err: context.Canceled}, "cancelled"}, |
| + {&url.Error{Op: "Get", URL: "https://x", Err: context.DeadlineExceeded}, "timeout"}, |
| + {&url.Error{Op: "Get", URL: "https://x", Err: errRefusedAddr}, "refused address"}, |
| + {&url.Error{Op: "Get", URL: "https://x", Err: errors.New("connection reset")}, "connect failed"}, |
| + } { |
| + if got := legalFetchErr(tc.err).Error(); got != tc.want { |
| + t.Errorf("%v: %q, want %q", tc.err, got, tc.want) |
| + } |
| + } |
| +} |