go/svc/speedtest: init from github.com/q3k/speedtest
commit: 6109ea9470199468120836d1fb1c89cac7667d86
Change-Id: If93617b3b8f0ac5bc548ab81a6801e464a03143d
diff --git a/go/svc/speedtest/backend/main_test.go b/go/svc/speedtest/backend/main_test.go
new file mode 100644
index 0000000..51bdfb4
--- /dev/null
+++ b/go/svc/speedtest/backend/main_test.go
@@ -0,0 +1,50 @@
+package main
+
+// Copyright 2019 Serge Bazanski
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
+// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+import (
+ "net/http"
+ "testing"
+)
+
+func TestRemoteIP(t *testing.T) {
+ cases := []struct {
+ in string
+ out string
+ }{
+ {"1.2.3.4", "1.2.3.4"},
+ {" 1.2.3.4 ", "1.2.3.4"},
+ {"1.2.3.4.5", ""},
+ {"1.2.3.4:8080", "1.2.3.4"},
+ {"fe80::2", "fe80::2"},
+ {"[fe80::2]:3213", "fe80::2"},
+ {"fe80::2:43214", ""},
+ }
+
+ for i, c := range cases {
+ r := http.Request{
+ RemoteAddr: c.in,
+ }
+ res := remoteIP(&r)
+ got := ""
+ if res != nil {
+ got = res.String()
+ }
+
+ if c.out != got {
+ t.Errorf("Test case %d: got %q, want %q", i, got, c.out)
+ }
+ }
+}