wildcard-hosts.bats 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/env bats
  2. load test_helpers
  3. SUT_CONTAINER=bats-nginx-proxy-${TEST_FILE}
  4. function setup {
  5. # make sure to stop any web container before each test so we don't
  6. # have any unexpected contaiener running with VIRTUAL_HOST or VIRUTAL_PORT set
  7. docker ps -q --filter "label=bats-type=web" | xargs -r docker stop >&2
  8. }
  9. @test "[$TEST_FILE] start a nginx-proxy container" {
  10. # GIVEN
  11. run nginxproxy $SUT_CONTAINER -v /var/run/docker.sock:/tmp/docker.sock:ro
  12. assert_success
  13. docker_wait_for_log $SUT_CONTAINER 3 "Watching docker events"
  14. }
  15. @test "[$TEST_FILE] VIRTUAL_HOST=*.wildcard.bats" {
  16. # WHEN
  17. prepare_web_container bats-wildcard-hosts-1 80 -e VIRTUAL_HOST=*.wildcard.bats
  18. # THEN
  19. assert_200 f00.wildcard.bats
  20. assert_200 bar.wildcard.bats
  21. assert_503 unexpected.host.bats
  22. }
  23. @test "[$TEST_FILE] VIRTUAL_HOST=wildcard.bats.*" {
  24. # WHEN
  25. prepare_web_container bats-wildcard-hosts-2 80 -e VIRTUAL_HOST=wildcard.bats.*
  26. # THEN
  27. assert_200 wildcard.bats.f00
  28. assert_200 wildcard.bats.bar
  29. assert_503 unexpected.host.bats
  30. }
  31. @test "[$TEST_FILE] VIRTUAL_HOST=~^foo\.bar\..*\.bats" {
  32. # WHEN
  33. prepare_web_container bats-wildcard-hosts-2 80 -e VIRTUAL_HOST=~^foo\.bar\..*\.bats
  34. # THEN
  35. assert_200 foo.bar.whatever.bats
  36. assert_200 foo.bar.why.not.bats
  37. assert_503 unexpected.host.bats
  38. }
  39. # assert that querying nginx-proxy with the given Host header produces a `HTTP 200` response
  40. # $1 Host HTTP header to use when querying nginx-proxy
  41. function assert_200 {
  42. local -r host=$1
  43. run curl_container $SUT_CONTAINER / --head --header "Host: $host"
  44. assert_output -l 0 $'HTTP/1.1 200 OK\r'
  45. }
  46. # assert that querying nginx-proxy with the given Host header produces a `HTTP 503` response
  47. # $1 Host HTTP header to use when querying nginx-proxy
  48. function assert_503 {
  49. local -r host=$1
  50. run curl_container $SUT_CONTAINER / --head --header "Host: $host"
  51. assert_output -l 0 $'HTTP/1.1 503 Service Temporarily Unavailable\r'
  52. }