about summary refs log tree commit homepage
diff options
context:
space:
mode:
-rw-r--r--lib/rainbows/server_token.rb39
-rwxr-xr-xt/t9002-server-token.sh37
-rw-r--r--t/t9002.ru4
3 files changed, 80 insertions, 0 deletions
diff --git a/lib/rainbows/server_token.rb b/lib/rainbows/server_token.rb
new file mode 100644
index 0000000..4098ca7
--- /dev/null
+++ b/lib/rainbows/server_token.rb
@@ -0,0 +1,39 @@
+# -*- encoding: binary -*-
+module Rainbows
+
+# An optional middleware to proudly display your usage of \Rainbows! in
+# the "Server:" response header.  This means you can help tell the world
+# you're using \Rainbows! and spread fun and joy all over the Internet!
+#
+#    ------ in your config.ru ------
+#    require 'rainbows/server_token'
+#    require 'rack/lobster'
+#    use Rainbows::ServerToken
+#    run Rack::Lobster.new
+#
+# If you're nervous about the exact version of \Rainbows! you're running,
+# then you can actually specify anything you want:
+#
+#    use Rainbows::ServerToken, "netcat 1.0"
+#
+
+class ServerToken < Struct.new(:app, :token)
+
+  # :stopdoc:
+  #
+  # Freeze constants as they're slightly faster when setting hashes
+  SERVER = "Server".freeze
+
+  def initialize(app, token = Const::RACK_DEFAULTS['SERVER_SOFTWARE'])
+    super
+  end
+
+  def call(env)
+    status, headers, body = app.call(env)
+    headers = Rack::Utils::HeaderHash.new(headers)
+    headers[SERVER] = token
+    [ status, headers, body ]
+  end
+  # :startdoc:
+end
+end
diff --git a/t/t9002-server-token.sh b/t/t9002-server-token.sh
new file mode 100755
index 0000000..914a717
--- /dev/null
+++ b/t/t9002-server-token.sh
@@ -0,0 +1,37 @@
+#!/bin/sh
+. ./test-lib.sh
+case $model in
+Base) ;;
+*) exit 0 ;; # don't waste cycles on trivial stuff :P
+esac
+
+t_plan 6 "ServerToken Rack middleware test for $model"
+
+t_begin "configure and start" && {
+        rtmpfiles curl_out curl_err
+        rainbows_setup
+        rainbows -D t9002.ru -c $unicorn_config
+        rainbows_wait_start
+}
+
+t_begin "hit with curl" && {
+        curl -sSfiI http://$listen/ > $curl_out 2> $curl_err
+}
+
+t_begin "kill server" && {
+        kill $rainbows_pid
+}
+
+t_begin "no errors in curl stderr" && {
+        test ! -s $curl_err
+}
+
+t_begin "no errors in Rainbows! stderr" && {
+        check_stderr
+}
+
+t_begin "Server: token added" && {
+        grep Server: $curl_out
+}
+
+t_done
diff --git a/t/t9002.ru b/t/t9002.ru
new file mode 100644
index 0000000..7f55824
--- /dev/null
+++ b/t/t9002.ru
@@ -0,0 +1,4 @@
+require 'rainbows/server_token'
+require 'rack/lobster'
+use Rainbows::ServerToken
+run Rack::Lobster.new