Merge branch 'sk/wincred'
[gitweb.git] / t / lib-httpd.sh
index ad8f1ef71efcd7e449d2cad8edd3cf80c98ca188..8b67021a6bb95c8a2e6ed2530267e338f2351082 100644 (file)
@@ -1,14 +1,47 @@
-#!/bin/sh
+# Shell library to run an HTTP server for use in tests.
+# Ends the test early if httpd tests should not be run,
+# for example because the user has not enabled them.
+#
+# Usage:
+#
+#      . ./test-lib.sh
+#      . "$TEST_DIRECTORY"/lib-httpd.sh
+#      start_httpd
+#
+#      test_expect_success '...' '
+#              ...
+#      '
+#
+#      test_expect_success ...
+#
+#      stop_httpd
+#      test_done
+#
+# Can be configured using the following variables.
+#
+#    GIT_TEST_HTTPD              enable HTTPD tests
+#    LIB_HTTPD_PATH              web server path
+#    LIB_HTTPD_MODULE_PATH       web server modules path
+#    LIB_HTTPD_PORT              listening port
+#    LIB_HTTPD_DAV               enable DAV
+#    LIB_HTTPD_SVN               enable SVN
+#    LIB_HTTPD_SSL               enable SSL
 #
 # Copyright (c) 2008 Clemens Buchacher <drizzd@aon.at>
 #
 
-if test -z "$GIT_TEST_HTTPD"
+test_tristate GIT_TEST_HTTPD
+if test "$GIT_TEST_HTTPD" = false
 then
-       skip_all="Network testing disabled (define GIT_TEST_HTTPD to enable)"
+       skip_all="Network testing disabled (unset GIT_TEST_HTTPD to enable)"
        test_done
 fi
 
+if ! test_have_prereq SANITY; then
+       test_skip_or_die $GIT_TEST_HTTPD \
+               "Cannot run httpd tests as root"
+fi
+
 HTTPD_PARA=""
 
 for DEFAULT_HTTPD_PATH in '/usr/sbin/httpd' '/usr/sbin/apache2'
@@ -37,7 +70,7 @@ case $(uname) in
 esac
 
 LIB_HTTPD_PATH=${LIB_HTTPD_PATH-"$DEFAULT_HTTPD_PATH"}
-LIB_HTTPD_PORT=${LIB_HTTPD_PORT-'8111'}
+LIB_HTTPD_PORT=${LIB_HTTPD_PORT-${this_test#t}}
 
 TEST_PATH="$TEST_DIRECTORY"/lib-httpd
 HTTPD_ROOT_PATH="$PWD"/httpd
@@ -49,8 +82,7 @@ GIT_VALGRIND_OPTIONS=$GIT_VALGRIND_OPTIONS; export GIT_VALGRIND_OPTIONS
 
 if ! test -x "$LIB_HTTPD_PATH"
 then
-       skip_all="skipping test, no web server found at '$LIB_HTTPD_PATH'"
-       test_done
+       test_skip_or_die $GIT_TEST_HTTPD "no web server found at '$LIB_HTTPD_PATH'"
 fi
 
 HTTPD_VERSION=`$LIB_HTTPD_PATH -v | \
@@ -62,19 +94,20 @@ then
        then
                if ! test $HTTPD_VERSION -ge 2
                then
-                       skip_all="skipping test, at least Apache version 2 is required"
-                       test_done
+                       test_skip_or_die $GIT_TEST_HTTPD \
+                               "at least Apache version 2 is required"
                fi
                if ! test -d "$DEFAULT_HTTPD_MODULE_PATH"
                then
-                       skip_all="Apache module directory not found.  Skipping tests."
-                       test_done
+                       test_skip_or_die $GIT_TEST_HTTPD \
+                               "Apache module directory not found"
                fi
 
                LIB_HTTPD_MODULE_PATH="$DEFAULT_HTTPD_MODULE_PATH"
        fi
 else
-       error "Could not identify web server at '$LIB_HTTPD_PATH'"
+       test_skip_or_die $GIT_TEST_HTTPD \
+               "Could not identify web server at '$LIB_HTTPD_PATH'"
 fi
 
 prepare_httpd() {
@@ -102,7 +135,7 @@ prepare_httpd() {
        HTTPD_DEST=127.0.0.1:$LIB_HTTPD_PORT
        HTTPD_URL=$HTTPD_PROTO://$HTTPD_DEST
        HTTPD_URL_USER=$HTTPD_PROTO://user%40host@$HTTPD_DEST
-       HTTPD_URL_USER_PASS=$HTTPD_PROTO://user%40host:user%40host@$HTTPD_DEST
+       HTTPD_URL_USER_PASS=$HTTPD_PROTO://user%40host:pass%40host@$HTTPD_DEST
 
        if test -n "$LIB_HTTPD_DAV" -o -n "$LIB_HTTPD_SVN"
        then
@@ -128,9 +161,8 @@ start_httpd() {
                >&3 2>&4
        if test $? -ne 0
        then
-               skip_all="skipping test, web server setup failed"
                trap 'die' EXIT
-               test_done
+               test_skip_or_die $GIT_TEST_HTTPD "web server setup failed"
        fi
 }
 
@@ -190,7 +222,15 @@ setup_askpass_helper() {
        test_expect_success 'setup askpass helper' '
                write_script "$TRASH_DIRECTORY/askpass" <<-\EOF &&
                echo >>"$TRASH_DIRECTORY/askpass-query" "askpass: $*" &&
-               cat "$TRASH_DIRECTORY/askpass-response"
+               case "$*" in
+               *Username*)
+                       what=user
+                       ;;
+               *Password*)
+                       what=pass
+                       ;;
+               esac &&
+               cat "$TRASH_DIRECTORY/askpass-$what"
                EOF
                GIT_ASKPASS="$TRASH_DIRECTORY/askpass" &&
                export GIT_ASKPASS &&
@@ -200,7 +240,8 @@ setup_askpass_helper() {
 
 set_askpass() {
        >"$TRASH_DIRECTORY/askpass-query" &&
-       echo "$*" >"$TRASH_DIRECTORY/askpass-response"
+       echo "$1" >"$TRASH_DIRECTORY/askpass-user" &&
+       echo "$2" >"$TRASH_DIRECTORY/askpass-pass"
 }
 
 expect_askpass() {