| 1 |
package op |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"log/slog" |
| 8 |
"net/http" |
| 9 |
"net/http/httptest" |
| 10 |
"net/url" |
| 11 |
"strings" |
| 12 |
"testing" |
| 13 |
|
| 14 |
"github.com/stretchr/testify/assert" |
| 15 |
"github.com/stretchr/testify/require" |
| 16 |
"github.com/zitadel/oidc/v3/pkg/oidc" |
| 17 |
"github.com/zitadel/schema" |
| 18 |
) |
| 19 |
|
| 20 |
// setDefaultTestLogger installs a process-wide default logger for the duration of t. |
| 21 |
// Do not use with t.Parallel(): slog.SetDefault is global and races across parallel tests. |
| 22 |
func setDefaultTestLogger(t *testing.T, logger *slog.Logger) { |
| 23 |
t.Helper() |
| 24 |
previous := slog.Default() |
| 25 |
slog.SetDefault(logger) |
| 26 |
t.Cleanup(func() { slog.SetDefault(previous) }) |
| 27 |
} |
| 28 |
|
| 29 |
func TestAuthRequestError(t *testing.T) { |
| 30 |
type args struct { |
| 31 |
authReq ErrAuthRequest |
| 32 |
err error |
| 33 |
} |
| 34 |
tests := []struct { |
| 35 |
name string |
| 36 |
args args |
| 37 |
wantCode int |
| 38 |
wantHeaders map[string]string |
| 39 |
wantBody string |
| 40 |
wantLog string |
| 41 |
}{ |
| 42 |
{ |
| 43 |
name: "nil auth request", |
| 44 |
args: args{ |
| 45 |
authReq: nil, |
| 46 |
err: io.ErrClosedPipe, |
| 47 |
}, |
| 48 |
wantCode: http.StatusBadRequest, |
| 49 |
wantBody: "io: read/write on closed pipe\n", |
| 50 |
wantLog: `{ |
| 51 |
"level":"ERROR", |
| 52 |
"msg":"auth request", |
| 53 |
"time":"not", |
| 54 |
"oidc_error":{ |
| 55 |
"description":"io: read/write on closed pipe", |
| 56 |
"parent":"io: read/write on closed pipe", |
| 57 |
"type":"server_error" |
| 58 |
} |
| 59 |
}`, |
| 60 |
}, |
| 61 |
{ |
| 62 |
name: "auth request, no redirect URI", |
| 63 |
args: args{ |
| 64 |
authReq: &oidc.AuthRequest{ |
| 65 |
Scopes: oidc.SpaceDelimitedArray{"a", "b"}, |
| 66 |
ResponseType: "responseType", |
| 67 |
ClientID: "123", |
| 68 |
State: "state1", |
| 69 |
ResponseMode: oidc.ResponseModeQuery, |
| 70 |
}, |
| 71 |
err: oidc.ErrInteractionRequired().WithDescription("sign in"), |
| 72 |
}, |
| 73 |
wantCode: http.StatusBadRequest, |
| 74 |
wantBody: "sign in\n", |
| 75 |
wantLog: `{ |
| 76 |
"level":"WARN", |
| 77 |
"msg":"auth request: not redirecting", |
| 78 |
"time":"not", |
| 79 |
"auth_request":{ |
| 80 |
"client_id":"123", |
| 81 |
"redirect_uri":"", |
| 82 |
"response_type":"responseType", |
| 83 |
"scopes":"a b" |
| 84 |
}, |
| 85 |
"oidc_error":{ |
| 86 |
"description":"sign in", |
| 87 |
"type":"interaction_required" |
| 88 |
} |
| 89 |
}`, |
| 90 |
}, |
| 91 |
{ |
| 92 |
name: "auth request, redirect disabled", |
| 93 |
args: args{ |
| 94 |
authReq: &oidc.AuthRequest{ |
| 95 |
Scopes: oidc.SpaceDelimitedArray{"a", "b"}, |
| 96 |
ResponseType: "responseType", |
| 97 |
ClientID: "123", |
| 98 |
RedirectURI: "http://example.com/callback", |
| 99 |
State: "state1", |
| 100 |
ResponseMode: oidc.ResponseModeQuery, |
| 101 |
}, |
| 102 |
err: oidc.ErrInvalidRequestRedirectURI().WithDescription("oops"), |
| 103 |
}, |
| 104 |
wantCode: http.StatusBadRequest, |
| 105 |
wantBody: "oops\n", |
| 106 |
wantLog: `{ |
| 107 |
"level":"WARN", |
| 108 |
"msg":"auth request: not redirecting", |
| 109 |
"time":"not", |
| 110 |
"auth_request":{ |
| 111 |
"client_id":"123", |
| 112 |
"redirect_uri":"http://example.com/callback", |
| 113 |
"response_type":"responseType", |
| 114 |
"scopes":"a b" |
| 115 |
}, |
| 116 |
"oidc_error":{ |
| 117 |
"description":"oops", |
| 118 |
"type":"invalid_request", |
| 119 |
"redirect_disabled":true |
| 120 |
} |
| 121 |
}`, |
| 122 |
}, |
| 123 |
{ |
| 124 |
name: "auth request, url parse error", |
| 125 |
args: args{ |
| 126 |
authReq: &oidc.AuthRequest{ |
| 127 |
Scopes: oidc.SpaceDelimitedArray{"a", "b"}, |
| 128 |
ResponseType: "responseType", |
| 129 |
ClientID: "123", |
| 130 |
RedirectURI: "can't parse this!\n", |
| 131 |
State: "state1", |
| 132 |
ResponseMode: oidc.ResponseModeQuery, |
| 133 |
}, |
| 134 |
err: oidc.ErrInteractionRequired().WithDescription("sign in"), |
| 135 |
}, |
| 136 |
wantCode: http.StatusBadRequest, |
| 137 |
wantBody: "ErrorType=server_error Parent=parse \"can't parse this!\\n\": net/url: invalid control character in URL\n", |
| 138 |
wantLog: `{ |
| 139 |
"level":"ERROR", |
| 140 |
"msg":"auth response URL", |
| 141 |
"time":"not", |
| 142 |
"auth_request":{ |
| 143 |
"client_id":"123", |
| 144 |
"redirect_uri":"can't parse this!\n", |
| 145 |
"response_type":"responseType", |
| 146 |
"scopes":"a b" |
| 147 |
}, |
| 148 |
"error":{ |
| 149 |
"type":"server_error", |
| 150 |
"parent":"parse \"can't parse this!\\n\": net/url: invalid control character in URL" |
| 151 |
}, |
| 152 |
"oidc_error":{ |
| 153 |
"description":"sign in", |
| 154 |
"state":"state1", |
| 155 |
"type":"interaction_required" |
| 156 |
} |
| 157 |
}`, |
| 158 |
}, |
| 159 |
{ |
| 160 |
name: "auth request redirect", |
| 161 |
args: args{ |
| 162 |
authReq: &oidc.AuthRequest{ |
| 163 |
Scopes: oidc.SpaceDelimitedArray{"a", "b"}, |
| 164 |
ResponseType: "responseType", |
| 165 |
ClientID: "123", |
| 166 |
RedirectURI: "http://example.com/callback", |
| 167 |
State: "state1", |
| 168 |
ResponseMode: oidc.ResponseModeQuery, |
| 169 |
}, |
| 170 |
err: oidc.ErrInteractionRequired().WithDescription("sign in"), |
| 171 |
}, |
| 172 |
wantCode: http.StatusFound, |
| 173 |
wantHeaders: map[string]string{"Location": "http://example.com/callback?error=interaction_required&error_description=sign+in&state=state1"}, |
| 174 |
wantLog: `{ |
| 175 |
"level":"WARN", |
| 176 |
"msg":"auth request", |
| 177 |
"time":"not", |
| 178 |
"auth_request":{ |
| 179 |
"client_id":"123", |
| 180 |
"redirect_uri":"http://example.com/callback", |
| 181 |
"response_type":"responseType", |
| 182 |
"scopes":"a b" |
| 183 |
}, |
| 184 |
"oidc_error":{ |
| 185 |
"description":"sign in", |
| 186 |
"state":"state1", |
| 187 |
"type":"interaction_required" |
| 188 |
} |
| 189 |
}`, |
| 190 |
}, |
| 191 |
} |
| 192 |
for _, tt := range tests { |
| 193 |
t.Run(tt.name, func(t *testing.T) { |
| 194 |
logOut := new(strings.Builder) |
| 195 |
logger := slog.New( |
| 196 |
slog.NewJSONHandler(logOut, &slog.HandlerOptions{ |
| 197 |
Level: slog.LevelInfo, |
| 198 |
}).WithAttrs([]slog.Attr{slog.String("time", "not")}), |
| 199 |
) |
| 200 |
setDefaultTestLogger(t, logger) |
| 201 |
authorizer := &Provider{ |
| 202 |
encoder: schema.NewEncoder(), |
| 203 |
} |
| 204 |
|
| 205 |
w := httptest.NewRecorder() |
| 206 |
r := httptest.NewRequest("POST", "/path", nil) |
| 207 |
AuthRequestError(w, r, tt.args.authReq, tt.args.err, authorizer) |
| 208 |
|
| 209 |
res := w.Result() |
| 210 |
defer res.Body.Close() |
| 211 |
|
| 212 |
assert.Equal(t, tt.wantCode, res.StatusCode) |
| 213 |
for key, wantHeader := range tt.wantHeaders { |
| 214 |
gotHeader := res.Header.Get(key) |
| 215 |
assert.Equalf(t, wantHeader, gotHeader, "header %q", key) |
| 216 |
} |
| 217 |
gotBody, err := io.ReadAll(res.Body) |
| 218 |
require.NoError(t, err, "read result body") |
| 219 |
assert.Equal(t, tt.wantBody, string(gotBody), "result body") |
| 220 |
|
| 221 |
gotLog := logOut.String() |
| 222 |
t.Log(gotLog) |
| 223 |
assert.JSONEq(t, tt.wantLog, gotLog, "log output") |
| 224 |
}) |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
func TestRequestError(t *testing.T) { |
| 229 |
tests := []struct { |
| 230 |
name string |
| 231 |
err error |
| 232 |
wantCode int |
| 233 |
wantBody string |
| 234 |
wantLog string |
| 235 |
}{ |
| 236 |
{ |
| 237 |
name: "server error", |
| 238 |
err: io.ErrClosedPipe, |
| 239 |
wantCode: http.StatusInternalServerError, |
| 240 |
wantBody: `{"error":"server_error", "error_description":"io: read/write on closed pipe"}`, |
| 241 |
wantLog: `{ |
| 242 |
"level":"ERROR", |
| 243 |
"msg":"request error", |
| 244 |
"time":"not", |
| 245 |
"oidc_error":{ |
| 246 |
"parent":"io: read/write on closed pipe", |
| 247 |
"description":"io: read/write on closed pipe", |
| 248 |
"type":"server_error"} |
| 249 |
}`, |
| 250 |
}, |
| 251 |
{ |
| 252 |
name: "invalid request", |
| 253 |
err: oidc.ErrInvalidRequest().WithDescription("missing param"), |
| 254 |
wantCode: http.StatusBadRequest, |
| 255 |
wantBody: `{"error":"invalid_request", "error_description":"missing param"}`, |
| 256 |
wantLog: `{ |
| 257 |
"level":"WARN", |
| 258 |
"msg":"request error", |
| 259 |
"time":"not", |
| 260 |
"oidc_error":{ |
| 261 |
"description":"missing param", |
| 262 |
"type":"invalid_request"} |
| 263 |
}`, |
| 264 |
}, |
| 265 |
{ |
| 266 |
name: "invalid client", |
| 267 |
err: oidc.ErrInvalidClient().WithDescription("not good"), |
| 268 |
wantCode: http.StatusUnauthorized, |
| 269 |
wantBody: `{"error":"invalid_client", "error_description":"not good"}`, |
| 270 |
wantLog: `{ |
| 271 |
"level":"WARN", |
| 272 |
"msg":"request error", |
| 273 |
"time":"not", |
| 274 |
"oidc_error":{ |
| 275 |
"description":"not good", |
| 276 |
"type":"invalid_client"} |
| 277 |
}`, |
| 278 |
}, |
| 279 |
} |
| 280 |
for _, tt := range tests { |
| 281 |
t.Run(tt.name, func(t *testing.T) { |
| 282 |
logOut := new(strings.Builder) |
| 283 |
logger := slog.New( |
| 284 |
slog.NewJSONHandler(logOut, &slog.HandlerOptions{ |
| 285 |
Level: slog.LevelInfo, |
| 286 |
}).WithAttrs([]slog.Attr{slog.String("time", "not")}), |
| 287 |
) |
| 288 |
setDefaultTestLogger(t, logger) |
| 289 |
w := httptest.NewRecorder() |
| 290 |
r := httptest.NewRequest("POST", "/path", nil) |
| 291 |
RequestError(w, r, tt.err, logger) |
| 292 |
|
| 293 |
res := w.Result() |
| 294 |
defer res.Body.Close() |
| 295 |
|
| 296 |
assert.Equal(t, tt.wantCode, res.StatusCode, "status code") |
| 297 |
|
| 298 |
gotBody, err := io.ReadAll(res.Body) |
| 299 |
require.NoError(t, err, "read result body") |
| 300 |
assert.JSONEq(t, tt.wantBody, string(gotBody), "result body") |
| 301 |
|
| 302 |
gotLog := logOut.String() |
| 303 |
t.Log(gotLog) |
| 304 |
assert.JSONEq(t, tt.wantLog, gotLog, "log output") |
| 305 |
}) |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
func TestTryErrorRedirect(t *testing.T) { |
| 310 |
type args struct { |
| 311 |
ctx context.Context |
| 312 |
authReq ErrAuthRequest |
| 313 |
parent error |
| 314 |
} |
| 315 |
tests := []struct { |
| 316 |
name string |
| 317 |
args args |
| 318 |
want *Redirect |
| 319 |
wantErr error |
| 320 |
wantLog string |
| 321 |
}{ |
| 322 |
{ |
| 323 |
name: "nil auth request", |
| 324 |
args: args{ |
| 325 |
ctx: context.Background(), |
| 326 |
authReq: nil, |
| 327 |
parent: io.ErrClosedPipe, |
| 328 |
}, |
| 329 |
wantErr: NewStatusError(io.ErrClosedPipe, http.StatusBadRequest), |
| 330 |
wantLog: `{ |
| 331 |
"level":"ERROR", |
| 332 |
"msg":"auth request", |
| 333 |
"time":"not", |
| 334 |
"oidc_error":{ |
| 335 |
"description":"io: read/write on closed pipe", |
| 336 |
"parent":"io: read/write on closed pipe", |
| 337 |
"type":"server_error" |
| 338 |
} |
| 339 |
}`, |
| 340 |
}, |
| 341 |
{ |
| 342 |
name: "auth request, no redirect URI", |
| 343 |
args: args{ |
| 344 |
ctx: context.Background(), |
| 345 |
authReq: &oidc.AuthRequest{ |
| 346 |
Scopes: oidc.SpaceDelimitedArray{"a", "b"}, |
| 347 |
ResponseType: "responseType", |
| 348 |
ClientID: "123", |
| 349 |
State: "state1", |
| 350 |
ResponseMode: oidc.ResponseModeQuery, |
| 351 |
}, |
| 352 |
parent: oidc.ErrInteractionRequired().WithDescription("sign in"), |
| 353 |
}, |
| 354 |
wantErr: NewStatusError(oidc.ErrInteractionRequired().WithDescription("sign in"), http.StatusBadRequest), |
| 355 |
wantLog: `{ |
| 356 |
"level":"WARN", |
| 357 |
"msg":"auth request: not redirecting", |
| 358 |
"time":"not", |
| 359 |
"auth_request":{ |
| 360 |
"client_id":"123", |
| 361 |
"redirect_uri":"", |
| 362 |
"response_type":"responseType", |
| 363 |
"scopes":"a b" |
| 364 |
}, |
| 365 |
"oidc_error":{ |
| 366 |
"description":"sign in", |
| 367 |
"type":"interaction_required" |
| 368 |
} |
| 369 |
}`, |
| 370 |
}, |
| 371 |
{ |
| 372 |
name: "auth request, redirect disabled", |
| 373 |
args: args{ |
| 374 |
ctx: context.Background(), |
| 375 |
authReq: &oidc.AuthRequest{ |
| 376 |
Scopes: oidc.SpaceDelimitedArray{"a", "b"}, |
| 377 |
ResponseType: "responseType", |
| 378 |
ClientID: "123", |
| 379 |
RedirectURI: "http://example.com/callback", |
| 380 |
State: "state1", |
| 381 |
ResponseMode: oidc.ResponseModeQuery, |
| 382 |
}, |
| 383 |
parent: oidc.ErrInvalidRequestRedirectURI().WithDescription("oops"), |
| 384 |
}, |
| 385 |
wantErr: NewStatusError(oidc.ErrInvalidRequestRedirectURI().WithDescription("oops"), http.StatusBadRequest), |
| 386 |
wantLog: `{ |
| 387 |
"level":"WARN", |
| 388 |
"msg":"auth request: not redirecting", |
| 389 |
"time":"not", |
| 390 |
"auth_request":{ |
| 391 |
"client_id":"123", |
| 392 |
"redirect_uri":"http://example.com/callback", |
| 393 |
"response_type":"responseType", |
| 394 |
"scopes":"a b" |
| 395 |
}, |
| 396 |
"oidc_error":{ |
| 397 |
"description":"oops", |
| 398 |
"type":"invalid_request", |
| 399 |
"redirect_disabled":true |
| 400 |
} |
| 401 |
}`, |
| 402 |
}, |
| 403 |
{ |
| 404 |
name: "auth request, url parse error", |
| 405 |
args: args{ |
| 406 |
ctx: context.Background(), |
| 407 |
authReq: &oidc.AuthRequest{ |
| 408 |
Scopes: oidc.SpaceDelimitedArray{"a", "b"}, |
| 409 |
ResponseType: "responseType", |
| 410 |
ClientID: "123", |
| 411 |
RedirectURI: "can't parse this!\n", |
| 412 |
State: "state1", |
| 413 |
ResponseMode: oidc.ResponseModeQuery, |
| 414 |
}, |
| 415 |
parent: oidc.ErrInteractionRequired().WithDescription("sign in"), |
| 416 |
}, |
| 417 |
wantErr: func() error { |
| 418 |
//lint:ignore SA1007 just recreating the error for testing |
| 419 |
_, err := url.Parse("can't parse this!\n") |
| 420 |
err = oidc.ErrServerError().WithParent(err) |
| 421 |
return NewStatusError(err, http.StatusBadRequest) |
| 422 |
}(), |
| 423 |
wantLog: `{ |
| 424 |
"level":"ERROR", |
| 425 |
"msg":"auth response URL", |
| 426 |
"time":"not", |
| 427 |
"auth_request":{ |
| 428 |
"client_id":"123", |
| 429 |
"redirect_uri":"can't parse this!\n", |
| 430 |
"response_type":"responseType", |
| 431 |
"scopes":"a b" |
| 432 |
}, |
| 433 |
"error":{ |
| 434 |
"type":"server_error", |
| 435 |
"parent":"parse \"can't parse this!\\n\": net/url: invalid control character in URL" |
| 436 |
}, |
| 437 |
"oidc_error":{ |
| 438 |
"description":"sign in", |
| 439 |
"state":"state1", |
| 440 |
"type":"interaction_required" |
| 441 |
} |
| 442 |
}`, |
| 443 |
}, |
| 444 |
{ |
| 445 |
name: "auth request redirect", |
| 446 |
args: args{ |
| 447 |
ctx: context.Background(), |
| 448 |
authReq: &oidc.AuthRequest{ |
| 449 |
Scopes: oidc.SpaceDelimitedArray{"a", "b"}, |
| 450 |
ResponseType: "responseType", |
| 451 |
ClientID: "123", |
| 452 |
RedirectURI: "http://example.com/callback", |
| 453 |
State: "state1", |
| 454 |
ResponseMode: oidc.ResponseModeQuery, |
| 455 |
}, |
| 456 |
parent: oidc.ErrInteractionRequired().WithDescription("sign in"), |
| 457 |
}, |
| 458 |
want: &Redirect{ |
| 459 |
Header: make(http.Header), |
| 460 |
URL: "http://example.com/callback?error=interaction_required&error_description=sign+in&state=state1", |
| 461 |
}, |
| 462 |
wantLog: `{ |
| 463 |
"level":"WARN", |
| 464 |
"msg":"auth request redirect", |
| 465 |
"time":"not", |
| 466 |
"auth_request":{ |
| 467 |
"client_id":"123", |
| 468 |
"redirect_uri":"http://example.com/callback", |
| 469 |
"response_type":"responseType", |
| 470 |
"scopes":"a b" |
| 471 |
}, |
| 472 |
"oidc_error":{ |
| 473 |
"description":"sign in", |
| 474 |
"state":"state1", |
| 475 |
"type":"interaction_required" |
| 476 |
}, |
| 477 |
"url":"http://example.com/callback?error=interaction_required&error_description=sign+in&state=state1" |
| 478 |
}`, |
| 479 |
}, |
| 480 |
} |
| 481 |
for _, tt := range tests { |
| 482 |
t.Run(tt.name, func(t *testing.T) { |
| 483 |
logOut := new(strings.Builder) |
| 484 |
logger := slog.New( |
| 485 |
slog.NewJSONHandler(logOut, &slog.HandlerOptions{ |
| 486 |
Level: slog.LevelInfo, |
| 487 |
}).WithAttrs([]slog.Attr{slog.String("time", "not")}), |
| 488 |
) |
| 489 |
setDefaultTestLogger(t, logger) |
| 490 |
encoder := schema.NewEncoder() |
| 491 |
|
| 492 |
got, err := TryErrorRedirect(tt.args.ctx, tt.args.authReq, tt.args.parent, encoder, logger) |
| 493 |
require.ErrorIs(t, err, tt.wantErr) |
| 494 |
assert.Equal(t, tt.want, got) |
| 495 |
|
| 496 |
gotLog := logOut.String() |
| 497 |
t.Log(gotLog) |
| 498 |
assert.JSONEq(t, tt.wantLog, gotLog, "log output") |
| 499 |
}) |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
func TestNewStatusError(t *testing.T) { |
| 504 |
err := NewStatusError(io.ErrClosedPipe, http.StatusInternalServerError) |
| 505 |
|
| 506 |
want := "Internal Server Error: io: read/write on closed pipe" |
| 507 |
got := fmt.Sprint(err) |
| 508 |
assert.Equal(t, want, got) |
| 509 |
} |
| 510 |
|
| 511 |
func TestAsStatusError(t *testing.T) { |
| 512 |
type args struct { |
| 513 |
err error |
| 514 |
statusCode int |
| 515 |
} |
| 516 |
tests := []struct { |
| 517 |
name string |
| 518 |
args args |
| 519 |
want string |
| 520 |
}{ |
| 521 |
{ |
| 522 |
name: "already status error", |
| 523 |
args: args{ |
| 524 |
err: NewStatusError(io.ErrClosedPipe, http.StatusInternalServerError), |
| 525 |
statusCode: http.StatusBadRequest, |
| 526 |
}, |
| 527 |
want: "Internal Server Error: io: read/write on closed pipe", |
| 528 |
}, |
| 529 |
{ |
| 530 |
name: "oidc error", |
| 531 |
args: args{ |
| 532 |
err: oidc.ErrAcrInvalid, |
| 533 |
statusCode: http.StatusBadRequest, |
| 534 |
}, |
| 535 |
want: "Bad Request: acr is invalid", |
| 536 |
}, |
| 537 |
} |
| 538 |
for _, tt := range tests { |
| 539 |
t.Run(tt.name, func(t *testing.T) { |
| 540 |
err := AsStatusError(tt.args.err, tt.args.statusCode) |
| 541 |
got := fmt.Sprint(err) |
| 542 |
assert.Equal(t, tt.want, got) |
| 543 |
}) |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
func TestStatusError_Unwrap(t *testing.T) { |
| 548 |
err := NewStatusError(io.ErrClosedPipe, http.StatusInternalServerError) |
| 549 |
require.ErrorIs(t, err, io.ErrClosedPipe) |
| 550 |
} |
| 551 |
|
| 552 |
func TestStatusError_Is(t *testing.T) { |
| 553 |
type args struct { |
| 554 |
err error |
| 555 |
} |
| 556 |
tests := []struct { |
| 557 |
name string |
| 558 |
args args |
| 559 |
want bool |
| 560 |
}{ |
| 561 |
{ |
| 562 |
name: "nil error", |
| 563 |
args: args{err: nil}, |
| 564 |
want: false, |
| 565 |
}, |
| 566 |
{ |
| 567 |
name: "other error", |
| 568 |
args: args{err: io.EOF}, |
| 569 |
want: false, |
| 570 |
}, |
| 571 |
{ |
| 572 |
name: "other parent", |
| 573 |
args: args{err: NewStatusError(io.EOF, http.StatusInternalServerError)}, |
| 574 |
want: false, |
| 575 |
}, |
| 576 |
{ |
| 577 |
name: "other status", |
| 578 |
args: args{err: NewStatusError(io.ErrClosedPipe, http.StatusInsufficientStorage)}, |
| 579 |
want: false, |
| 580 |
}, |
| 581 |
{ |
| 582 |
name: "same", |
| 583 |
args: args{err: NewStatusError(io.ErrClosedPipe, http.StatusInternalServerError)}, |
| 584 |
want: true, |
| 585 |
}, |
| 586 |
{ |
| 587 |
name: "wrapped", |
| 588 |
args: args{err: fmt.Errorf("wrap: %w", NewStatusError(io.ErrClosedPipe, http.StatusInternalServerError))}, |
| 589 |
want: true, |
| 590 |
}, |
| 591 |
} |
| 592 |
for _, tt := range tests { |
| 593 |
t.Run(tt.name, func(t *testing.T) { |
| 594 |
e := NewStatusError(io.ErrClosedPipe, http.StatusInternalServerError) |
| 595 |
if got := e.Is(tt.args.err); got != tt.want { |
| 596 |
t.Errorf("StatusError.Is() = %v, want %v", got, tt.want) |
| 597 |
} |
| 598 |
}) |
| 599 |
} |
| 600 |
} |
| 601 |
|
| 602 |
func TestWriteError(t *testing.T) { |
| 603 |
tests := []struct { |
| 604 |
name string |
| 605 |
err error |
| 606 |
wantStatus int |
| 607 |
wantBody string |
| 608 |
wantLog string |
| 609 |
}{ |
| 610 |
{ |
| 611 |
name: "not a status or oidc error", |
| 612 |
err: io.ErrClosedPipe, |
| 613 |
wantStatus: http.StatusInternalServerError, |
| 614 |
wantBody: `{ |
| 615 |
"error":"server_error", |
| 616 |
"error_description":"io: read/write on closed pipe" |
| 617 |
}`, |
| 618 |
wantLog: `{ |
| 619 |
"level":"ERROR", |
| 620 |
"msg":"request error", |
| 621 |
"oidc_error":{ |
| 622 |
"description":"io: read/write on closed pipe", |
| 623 |
"parent":"io: read/write on closed pipe", |
| 624 |
"type":"server_error" |
| 625 |
}, |
| 626 |
"status_code":500, |
| 627 |
"time":"not" |
| 628 |
}`, |
| 629 |
}, |
| 630 |
{ |
| 631 |
name: "status error w/o oidc", |
| 632 |
err: NewStatusError(io.ErrClosedPipe, http.StatusInternalServerError), |
| 633 |
wantStatus: http.StatusInternalServerError, |
| 634 |
wantBody: `{ |
| 635 |
"error":"server_error", |
| 636 |
"error_description":"io: read/write on closed pipe" |
| 637 |
}`, |
| 638 |
wantLog: `{ |
| 639 |
"level":"ERROR", |
| 640 |
"msg":"request error", |
| 641 |
"oidc_error":{ |
| 642 |
"description":"io: read/write on closed pipe", |
| 643 |
"parent":"io: read/write on closed pipe", |
| 644 |
"type":"server_error" |
| 645 |
}, |
| 646 |
"status_code":500, |
| 647 |
"time":"not" |
| 648 |
}`, |
| 649 |
}, |
| 650 |
{ |
| 651 |
name: "oidc error w/o status", |
| 652 |
err: oidc.ErrInvalidRequest().WithDescription("oops"), |
| 653 |
wantStatus: http.StatusBadRequest, |
| 654 |
wantBody: `{ |
| 655 |
"error":"invalid_request", |
| 656 |
"error_description":"oops" |
| 657 |
}`, |
| 658 |
wantLog: `{ |
| 659 |
"level":"WARN", |
| 660 |
"msg":"request error", |
| 661 |
"oidc_error":{ |
| 662 |
"description":"oops", |
| 663 |
"type":"invalid_request" |
| 664 |
}, |
| 665 |
"status_code":400, |
| 666 |
"time":"not" |
| 667 |
}`, |
| 668 |
}, |
| 669 |
{ |
| 670 |
name: "status with oidc error", |
| 671 |
err: NewStatusError( |
| 672 |
oidc.ErrUnauthorizedClient().WithDescription("oops"), |
| 673 |
http.StatusUnauthorized, |
| 674 |
), |
| 675 |
wantStatus: http.StatusUnauthorized, |
| 676 |
wantBody: `{ |
| 677 |
"error":"unauthorized_client", |
| 678 |
"error_description":"oops" |
| 679 |
}`, |
| 680 |
wantLog: `{ |
| 681 |
"level":"WARN", |
| 682 |
"msg":"request error", |
| 683 |
"oidc_error":{ |
| 684 |
"description":"oops", |
| 685 |
"type":"unauthorized_client" |
| 686 |
}, |
| 687 |
"status_code":401, |
| 688 |
"time":"not" |
| 689 |
}`, |
| 690 |
}, |
| 691 |
} |
| 692 |
for _, tt := range tests { |
| 693 |
t.Run(tt.name, func(t *testing.T) { |
| 694 |
logOut := new(strings.Builder) |
| 695 |
logger := slog.New( |
| 696 |
slog.NewJSONHandler(logOut, &slog.HandlerOptions{ |
| 697 |
Level: slog.LevelInfo, |
| 698 |
}).WithAttrs([]slog.Attr{slog.String("time", "not")}), |
| 699 |
) |
| 700 |
setDefaultTestLogger(t, logger) |
| 701 |
r := httptest.NewRequest("GET", "/target", nil) |
| 702 |
w := httptest.NewRecorder() |
| 703 |
|
| 704 |
WriteError(w, r, tt.err, logger) |
| 705 |
res := w.Result() |
| 706 |
assert.Equal(t, tt.wantStatus, res.StatusCode, "status code") |
| 707 |
gotBody, err := io.ReadAll(res.Body) |
| 708 |
require.NoError(t, err) |
| 709 |
assert.JSONEq(t, tt.wantBody, string(gotBody), "body") |
| 710 |
assert.JSONEq(t, tt.wantLog, logOut.String()) |
| 711 |
}) |
| 712 |
} |
| 713 |
} |