<commit_before>#!/usr/bin/env bats
load test_helpers
SUT_CONTAINER=bats-nginx-proxy-${TEST_FILE}

function setup {
	# make sure to stop any web container before each test so we don't
	# have any unexpected contaiener running with VIRTUAL_HOST or VIRUTAL_PORT set
	stop_bats_containers web
}


@test "[$TEST_FILE] start a nginx-proxy container" {
	# GIVEN nginx-proxy
	run nginxproxy $SUT_CONTAINER -v /var/run/docker.sock:/tmp/docker.sock:ro
	assert_success
	docker_wait_for_log $SUT_CONTAINER 9 "Watching docker events"
}


@test "[$TEST_FILE] nginx-proxy defaults to the service running on port 80" {
	# WHEN
	prepare_web_container bats-web-${TEST_FILE}-1 "80 90" -e VIRTUAL_HOST=web.bats
	dockergen_wait_for_event $SUT_CONTAINER start bats-web-${TEST_FILE}-1
	sleep 1

	# THEN
	assert_response_is_from_port 80
}


@test "[$TEST_FILE] VIRTUAL_PORT=90 while port 80 is also exposed" {
	# GIVEN
	prepare_web_container bats-web-${TEST_FILE}-2 "80 90" -e VIRTUAL_HOST=web.bats -e VIRTUAL_PORT=90
	dockergen_wait_for_event $SUT_CONTAINER start bats-web-${TEST_FILE}-2
	sleep 1

	# THEN
	assert_response_is_from_port 90
}


@test "[$TEST_FILE] VIRTUAL_HOST proxy syntax" {
	# GIVEN
	prepare_web_container bats-web-${TEST_FILE}-2 "80 90" -e VIRTUAL_HOST=web.bats=>http:80,web1.bats=>http:90

	# THEN
	assert_response_is_from_port 80 web.bats
	assert_response_is_from_port 90 web1.bats
}


@test "[$TEST_FILE] single exposed port != 80" {
	# GIVEN
	prepare_web_container bats-web-${TEST_FILE}-3 1234 -e VIRTUAL_HOST=web.bats
	dockergen_wait_for_event $SUT_CONTAINER start bats-web-${TEST_FILE}-3
	sleep 1

	# THEN
	assert_response_is_from_port 1234
}

@test "[$TEST_FILE] stop all bats containers" {
	stop_bats_containers
}


# assert querying nginx-proxy provides a response from the expected port of the web container
# $1 port we are expecting an response from
function assert_response_is_from_port {
	local -r port=$1
	local -r host=${2:-web.bats}
	run curl_container $SUT_CONTAINER /data --header "Host: $host"
	assert_output "answer from port $port"
}

<commit_msg>put the VIRTUAL_HOST value in quotes<commit_after>#!/usr/bin/env bats
load test_helpers
SUT_CONTAINER=bats-nginx-proxy-${TEST_FILE}

function setup {
	# make sure to stop any web container before each test so we don't
	# have any unexpected contaiener running with VIRTUAL_HOST or VIRUTAL_PORT set
	stop_bats_containers web
}


@test "[$TEST_FILE] start a nginx-proxy container" {
	# GIVEN nginx-proxy
	run nginxproxy $SUT_CONTAINER -v /var/run/docker.sock:/tmp/docker.sock:ro
	assert_success
	docker_wait_for_log $SUT_CONTAINER 9 "Watching docker events"
}


@test "[$TEST_FILE] nginx-proxy defaults to the service running on port 80" {
	# WHEN
	prepare_web_container bats-web-${TEST_FILE}-1 "80 90" -e VIRTUAL_HOST=web.bats
	dockergen_wait_for_event $SUT_CONTAINER start bats-web-${TEST_FILE}-1
	sleep 1

	# THEN
	assert_response_is_from_port 80
}


@test "[$TEST_FILE] VIRTUAL_PORT=90 while port 80 is also exposed" {
	# GIVEN
	prepare_web_container bats-web-${TEST_FILE}-2 "80 90" -e VIRTUAL_HOST=web.bats -e VIRTUAL_PORT=90
	dockergen_wait_for_event $SUT_CONTAINER start bats-web-${TEST_FILE}-2
	sleep 1

	# THEN
	assert_response_is_from_port 90
}


@test "[$TEST_FILE] VIRTUAL_HOST proxy syntax" {
	# GIVEN
	prepare_web_container bats-web-${TEST_FILE}-2 "80 90" -e VIRTUAL_HOST='web.bats=>http:80,web1.bats=>http:90'

	# THEN
	assert_response_is_from_port 80 web.bats
	assert_response_is_from_port 90 web1.bats
}


@test "[$TEST_FILE] single exposed port != 80" {
	# GIVEN
	prepare_web_container bats-web-${TEST_FILE}-3 1234 -e VIRTUAL_HOST=web.bats
	dockergen_wait_for_event $SUT_CONTAINER start bats-web-${TEST_FILE}-3
	sleep 1

	# THEN
	assert_response_is_from_port 1234
}

@test "[$TEST_FILE] stop all bats containers" {
	stop_bats_containers
}


# assert querying nginx-proxy provides a response from the expected port of the web container
# $1 port we are expecting an response from
function assert_response_is_from_port {
	local -r port=$1
	local -r host=${2:-web.bats}
	run curl_container $SUT_CONTAINER /data --header "Host: $host"
	assert_output "answer from port $port"
}


{'fraction_non_alphanumeric': 0.09675090252707581, 'fraction_numerical': 0.027918170878459686, 'mean_word_length': 5.1117647058823525, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6167635', 'n_tokens_mistral': 1738, 'n_tokens_neox': 1563, 'n_words': 395}