| 543 | } |
| 544 | |
| 545 | func TestProxy_ServeHTTP(t *testing.T) { |
| 546 | p := &Proxy{ |
| 547 | Client: &http.Client{ |
| 548 | Transport: &testTransport{}, |
| 549 | }, |
| 550 | AllowHosts: []string{"good.test"}, |
| 551 | ContentTypes: []string{"image/*"}, |
| 552 | } |
| 553 | |
| 554 | tests := []struct { |
| 555 | url string // request URL |
| 556 | code int // expected response status code |
| 557 | }{ |
| 558 | {"/favicon.ico", http.StatusOK}, |
| 559 | {"//foo", http.StatusBadRequest}, // invalid request URL |
| 560 | {"/http://bad.test/", http.StatusForbidden}, // Disallowed host |
| 561 | {"/http://good.test/error", http.StatusInternalServerError}, // HTTP protocol error |
| 562 | {"/http://good.test/nocontent", http.StatusNoContent}, // non-OK response |
| 563 | {"/100/http://good.test/png", http.StatusOK}, |
| 564 | {"/100/http://good.test/plain", http.StatusForbidden}, // non-image response |
| 565 | |
| 566 | // health-check URLs |
| 567 | {"/", http.StatusOK}, |
| 568 | {"/health-check", http.StatusOK}, |
| 569 | } |
| 570 | |
| 571 | for _, tt := range tests { |
| 572 | req, _ := http.NewRequest("GET", "http://localhost"+tt.url, nil) |
| 573 | resp := httptest.NewRecorder() |
| 574 | p.ServeHTTP(resp, req) |
| 575 | |
| 576 | if got, want := resp.Code, tt.code; got != want { |
| 577 | t.Errorf("ServeHTTP(%v) returned status %d, want %d", req, got, want) |
| 578 | } |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | // test that 304 Not Modified responses are returned properly. |
| 583 | func TestProxy_ServeHTTP_is304(t *testing.T) { |