2
0

ssl.bats 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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" \
  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" \
  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" \
  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] test SSL Strict-Transport-Security" {
  50. # WHEN
  51. prepare_web_container bats-ssl-hosts-4 "80" \
  52. -e VIRTUAL_HOST=*.nginx-proxy.bats \
  53. -e CERT_NAME=nginx-proxy.bats
  54. dockergen_wait_for_event $SUT_CONTAINER start bats-ssl-hosts-1
  55. sleep 1
  56. # THEN
  57. assert_301 test.nginx-proxy.bats
  58. assert_200_https test.nginx-proxy.bats
  59. assert_output -p "Strict-Transport-Security: max-age=31536000"
  60. }
  61. @test "[$TEST_FILE] test HTTPS_METHOD=noredirect disables Strict-Transport-Security" {
  62. # WHEN
  63. prepare_web_container bats-ssl-hosts-5 "80" \
  64. -e VIRTUAL_HOST=*.nginx-proxy.bats \
  65. -e CERT_NAME=nginx-proxy.bats \
  66. -e HTTPS_METHOD=noredirect
  67. dockergen_wait_for_event $SUT_CONTAINER start bats-ssl-hosts-3
  68. sleep 1
  69. # THEN
  70. assert_200 test.nginx-proxy.bats
  71. assert_200_https test.nginx-proxy.bats
  72. refute_output -p "Strict-Transport-Security: max-age=31536000"
  73. }
  74. @test "[$TEST_FILE] test HTTPS_METHOD=nohttps" {
  75. # WHEN
  76. prepare_web_container bats-ssl-hosts-6 "80" \
  77. -e VIRTUAL_HOST=*.nginx-proxy.bats \
  78. -e CERT_NAME=nginx-proxy.bats \
  79. -e HTTPS_METHOD=nohttps
  80. dockergen_wait_for_event $SUT_CONTAINER start bats-ssl-hosts-6
  81. sleep 1
  82. # THEN
  83. assert_down_https test.nginx-proxy.bats
  84. assert_200 test.nginx-proxy.bats
  85. }
  86. @test "[$TEST_FILE] stop all bats containers" {
  87. stop_bats_containers
  88. }
  89. # assert that querying nginx-proxy with the given Host header produces a `HTTP 200` response
  90. # $1 Host HTTP header to use when querying nginx-proxy
  91. function assert_200 {
  92. local -r host=$1
  93. run curl_container $SUT_CONTAINER / --head --header "Host: $host"
  94. assert_output -l 0 $'HTTP/1.1 200 OK\r'
  95. }
  96. # assert that querying nginx-proxy with the given Host header produces a `HTTP 503` response
  97. # $1 Host HTTP header to use when querying nginx-proxy
  98. function assert_503 {
  99. local -r host=$1
  100. run curl_container $SUT_CONTAINER / --head --header "Host: $host"
  101. assert_output -l 0 $'HTTP/1.1 503 Service Temporarily Unavailable\r'
  102. }
  103. # assert that querying nginx-proxy with the given Host header produces a `HTTP 503` response
  104. # $1 Host HTTP header to use when querying nginx-proxy
  105. function assert_301 {
  106. local -r host=$1
  107. run curl_container $SUT_CONTAINER / --head --header "Host: $host"
  108. assert_output -l 0 $'HTTP/1.1 301 Moved Permanently\r'
  109. }
  110. # assert that querying nginx-proxy with the given Host header fails because the host is down
  111. # $1 Host HTTP header to use when querying nginx-proxy
  112. function assert_down_https {
  113. local -r host=$1
  114. run curl_container_https $SUT_CONTAINER / --head --header "Host: $host"
  115. assert_failure
  116. }
  117. # assert that querying nginx-proxy with the given Host header produces a `HTTP 200` response
  118. # $1 Host HTTP header to use when querying nginx-proxy
  119. function assert_200_https {
  120. local -r host=$1
  121. run curl_container_https $SUT_CONTAINER / --head --header "Host: $host"
  122. assert_output -l 0 $'HTTP/1.1 200 OK\r'
  123. }
  124. # assert that querying nginx-proxy with the given Host header produces a `HTTP 503` response
  125. # $1 Host HTTP header to use when querying nginx-proxy
  126. function assert_503_https {
  127. local -r host=$1
  128. run curl_container_https $SUT_CONTAINER / --head --header "Host: $host"
  129. assert_output -l 0 $'HTTP/1.1 503 Service Temporarily Unavailable\r'
  130. }
  131. # assert that querying nginx-proxy with the given Host header produces a `HTTP 503` response
  132. # $1 Host HTTP header to use when querying nginx-proxy
  133. function assert_301_https {
  134. local -r host=$1
  135. run curl_container_https $SUT_CONTAINER / --head --header "Host: $host"
  136. assert_output -l 0 $'HTTP/1.1 301 Moved Permanently\r'
  137. }