2
0

ssl.bats 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. stop_bats_containers web
  8. }
  9. @test "[$TEST_FILE] start a nginx-proxy container" {
  10. run nginxproxy $SUT_CONTAINER -v /var/run/docker.sock:/tmp/docker.sock:ro -v ${DIR}/lib/ssl:/etc/nginx/certs:ro
  11. assert_success
  12. docker_wait_for_log $SUT_CONTAINER 9 "Watching docker events"
  13. }
  14. @test "[$TEST_FILE] test SSL for VIRTUAL_HOST=*.nginx-proxy.bats" {
  15. # WHEN
  16. prepare_web_container bats-ssl-hosts-1 "80 443" \
  17. -e VIRTUAL_HOST=*.nginx-proxy.bats \
  18. -e CERT_NAME=nginx-proxy.bats
  19. dockergen_wait_for_event $SUT_CONTAINER start bats-ssl-hosts-1
  20. sleep 1
  21. # THEN
  22. assert_301 test.nginx-proxy.bats
  23. assert_200_https test.nginx-proxy.bats
  24. }
  25. @test "[$TEST_FILE] test HTTPS_METHOD=nohttp" {
  26. # WHEN
  27. prepare_web_container bats-ssl-hosts-2 "80 443" \
  28. -e VIRTUAL_HOST=*.nginx-proxy.bats \
  29. -e CERT_NAME=nginx-proxy.bats \
  30. -e HTTPS_METHOD=nohttp
  31. dockergen_wait_for_event $SUT_CONTAINER start bats-ssl-hosts-2
  32. sleep 1
  33. # THEN
  34. assert_503 test.nginx-proxy.bats
  35. assert_200_https test.nginx-proxy.bats
  36. }
  37. @test "[$TEST_FILE] test HTTPS_METHOD=noredirect" {
  38. # WHEN
  39. prepare_web_container bats-ssl-hosts-3 "80 443" \
  40. -e VIRTUAL_HOST=*.nginx-proxy.bats \
  41. -e CERT_NAME=nginx-proxy.bats \
  42. -e HTTPS_METHOD=noredirect
  43. dockergen_wait_for_event $SUT_CONTAINER start bats-ssl-hosts-3
  44. sleep 1
  45. # THEN
  46. assert_200 test.nginx-proxy.bats
  47. assert_200_https test.nginx-proxy.bats
  48. }
  49. @test "[$TEST_FILE] stop all bats containers" {
  50. stop_bats_containers
  51. }
  52. # assert that querying nginx-proxy with the given Host header produces a `HTTP 200` response
  53. # $1 Host HTTP header to use when querying nginx-proxy
  54. function assert_200 {
  55. local -r host=$1
  56. run curl_container $SUT_CONTAINER / --head --header "Host: $host"
  57. assert_output -l 0 $'HTTP/1.1 200 OK\r'
  58. }
  59. # assert that querying nginx-proxy with the given Host header produces a `HTTP 503` response
  60. # $1 Host HTTP header to use when querying nginx-proxy
  61. function assert_503 {
  62. local -r host=$1
  63. run curl_container $SUT_CONTAINER / --head --header "Host: $host"
  64. assert_output -l 0 $'HTTP/1.1 503 Service Temporarily Unavailable\r'
  65. }
  66. # assert that querying nginx-proxy with the given Host header produces a `HTTP 503` response
  67. # $1 Host HTTP header to use when querying nginx-proxy
  68. function assert_301 {
  69. local -r host=$1
  70. run curl_container $SUT_CONTAINER / --head --header "Host: $host"
  71. assert_output -l 0 $'HTTP/1.1 301 Moved Permanently\r'
  72. }
  73. # assert that querying nginx-proxy with the given Host header produces a `HTTP 200` response
  74. # $1 Host HTTP header to use when querying nginx-proxy
  75. function assert_200_https {
  76. local -r host=$1
  77. run curl_container_https $SUT_CONTAINER / --head --header "Host: $host"
  78. assert_output -l 0 $'HTTP/1.1 200 OK\r'
  79. }
  80. # assert that querying nginx-proxy with the given Host header produces a `HTTP 503` response
  81. # $1 Host HTTP header to use when querying nginx-proxy
  82. function assert_503_https {
  83. local -r host=$1
  84. run curl_container_https $SUT_CONTAINER / --head --header "Host: $host"
  85. assert_output -l 0 $'HTTP/1.1 503 Service Temporarily Unavailable\r'
  86. }
  87. # assert that querying nginx-proxy with the given Host header produces a `HTTP 503` response
  88. # $1 Host HTTP header to use when querying nginx-proxy
  89. function assert_301_https {
  90. local -r host=$1
  91. run curl_container_https $SUT_CONTAINER / --head --header "Host: $host"
  92. assert_output -l 0 $'HTTP/1.1 301 Moved Permanently\r'
  93. }