#!/bin/bash
# Script to reproduce JWS-319

# deploy sample app to test with
cp ./output/build/webapps/docs/appdev/sample/sample.war ./output/build/webapps/

# configure server.xml to use JIO instead of the NIO default in tomcat8
sed -i 's@protocol="HTTP/1.1"@protocol="org.apache.coyote.http11.Http11Protocol"@g' output/build/conf/server.xml

# start the server
./output/build/bin/startup.sh

result=$(curl -Is --cookie-jar cookie.txt --header "Connection: keep-alive" http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp http://localhost:8080/sample/hello.jsp | grep "400 Bad Request" -c)

# check 400 request count
if [ $result -gt 0 ]; then
    echo "Failed."
else
    echo "Pass."
fi
