From d54da99667daae5efcbb103e62879d421057d0ef Mon Sep 17 00:00:00 2001 From: Dan Carley Date: Sun, 20 Oct 2013 13:54:20 +0100 Subject: [PATCH] Unit test the parsing of -header arguments Per the features described in the preceding commit. --- replay/settings_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/replay/settings_test.go b/replay/settings_test.go index 82e3fa6..c8916a9 100644 --- a/replay/settings_test.go +++ b/replay/settings_test.go @@ -1,6 +1,8 @@ package replay import ( + "flag" + "reflect" "testing" ) @@ -69,3 +71,41 @@ func TestElasticSearchSettings(t *testing.T) { t.Error("Index not match") } } + +func TestAdditionalHeaders(t *testing.T) { + args := []string{ + "-header", "Empty:", + "-header", "Foo: contains:multiple:colons", + "-header", "Host:nospaces.example.com", + "-header", "Authorization: Basic Zm9vOmJhcg==", + "-header", " User-Agent : Contains leading and trailing space ", + } + out := Headers{ + {"Empty", ""}, + {"Foo", "contains:multiple:colons"}, + {"Host", "nospaces.example.com"}, + {"Authorization", "Basic Zm9vOmJhcg=="}, + {"User-Agent", "Contains leading and trailing space"}, + } + + headers := Headers{} + fs := flag.NewFlagSet("TestHeaders", flag.ExitOnError) + fs.Var(&headers, "header", "blah") + fs.Parse(args) + + if !reflect.DeepEqual(headers, out) { + t.Error("Headers not parsed as expected") + } + + args = []string{ + "-header", "contains no colons", + } + headers = Headers{} + fs = flag.NewFlagSet("TestHeaders", flag.ContinueOnError) + fs.Var(&headers, "header", "blah") + err := fs.Parse(args) + + if err == nil { + t.Error("Invalid header should be rejected") + } +}