proxy_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package proxy
  2. import (
  3. "net/http"
  4. "net/url"
  5. "reflect"
  6. "testing"
  7. )
  8. func TestNewDirector(t *testing.T) {
  9. tests := []struct {
  10. good bool
  11. endpoints []string
  12. }{
  13. {true, []string{"http://192.0.2.8"}},
  14. {true, []string{"http://192.0.2.8:8001"}},
  15. {true, []string{"http://example.com"}},
  16. {true, []string{"http://example.com:8001"}},
  17. {true, []string{"http://192.0.2.8:8001", "http://example.com:8002"}},
  18. {false, []string{"192.0.2.8"}},
  19. {false, []string{"192.0.2.8:8001"}},
  20. {false, []string{""}},
  21. }
  22. for i, tt := range tests {
  23. _, err := newDirector(tt.endpoints)
  24. if tt.good != (err == nil) {
  25. t.Errorf("#%d: expected success = %t, got err = %v", i, tt.good, err)
  26. }
  27. }
  28. }
  29. func TestDirectorDirect(t *testing.T) {
  30. d := &director{
  31. endpoints: []url.URL{
  32. url.URL{
  33. Scheme: "http",
  34. Host: "bar.example.com",
  35. },
  36. },
  37. }
  38. req := &http.Request{
  39. Method: "GET",
  40. Host: "foo.example.com",
  41. URL: &url.URL{
  42. Host: "foo.example.com",
  43. Path: "/v2/keys/baz",
  44. },
  45. }
  46. d.direct(req)
  47. want := &http.Request{
  48. Method: "GET",
  49. // this field must not change
  50. Host: "foo.example.com",
  51. URL: &url.URL{
  52. // the Scheme field is updated per the director's first endpoint
  53. Scheme: "http",
  54. // the Host field is updated per the director's first endpoint
  55. Host: "bar.example.com",
  56. Path: "/v2/keys/baz",
  57. },
  58. }
  59. if !reflect.DeepEqual(want, req) {
  60. t.Fatalf("HTTP request does not match expected criteria: want=%#v got=%#v", want, req)
  61. }
  62. }