From 7d42369a655222c8774d51f63eb96e781f4c5f93 Mon Sep 17 00:00:00 2001
From: Will McCutchen <will@mccutch.org>
Date: Mon, 2 Jan 2017 12:37:09 -0800
Subject: [PATCH] Add tests for helpers

---
 httpbin/helpers_test.go | 55 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)
 create mode 100644 httpbin/helpers_test.go

diff --git a/httpbin/helpers_test.go b/httpbin/helpers_test.go
new file mode 100644
index 0000000..1422279
--- /dev/null
+++ b/httpbin/helpers_test.go
@@ -0,0 +1,55 @@
+package httpbin
+
+import (
+	"fmt"
+	"testing"
+	"time"
+)
+
+func TestParseDuration(t *testing.T) {
+	var okTests = []struct {
+		input    string
+		expected time.Duration
+	}{
+		// go-style durations
+		{"1s", time.Second},
+		{"500ms", 500 * time.Millisecond},
+		{"1.5h", 90 * time.Minute},
+		{"-10m", -10 * time.Minute},
+
+		// or floating point seconds
+		{"1", time.Second},
+		{"0.25", 250 * time.Millisecond},
+		{"-25", -25 * time.Second},
+		{"-2.5", -2500 * time.Millisecond},
+	}
+	for _, test := range okTests {
+		t.Run(fmt.Sprintf("ok/%s", test.input), func(t *testing.T) {
+			result, err := parseDuration(test.input)
+			if err != nil {
+				t.Fatalf("unexpected error parsing duration %v: %s", test.input, err)
+			}
+			if result != test.expected {
+				t.Fatalf("expected %s, got %s", test.expected, result)
+			}
+		})
+	}
+
+	var badTests = []struct {
+		input string
+	}{
+		{"foo"},
+		{"100foo"},
+		{"1/1"},
+		{"1.5.foo"},
+		{"0xFF"},
+	}
+	for _, test := range badTests {
+		t.Run(fmt.Sprintf("bad/%s", test.input), func(t *testing.T) {
+			_, err := parseDuration(test.input)
+			if err == nil {
+				t.Fatalf("expected error parsing %v", test.input)
+			}
+		})
+	}
+}
-- 
GitLab