blob: 51bdfb4917a7d2b0978fa5bb552180de9fd4efbe [file] [log] [blame]
Sergiusz Bazanskia51df9c2019-07-13 16:07:13 +02001package main
2
3// Copyright 2019 Serge Bazanski
4//
5// Permission to use, copy, modify, and/or distribute this software for any
6// purpose with or without fee is hereby granted, provided that the above
7// copyright notice and this permission notice appear in all copies.
8//
9// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
12// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
14// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
15// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
17import (
18 "net/http"
19 "testing"
20)
21
22func TestRemoteIP(t *testing.T) {
23 cases := []struct {
24 in string
25 out string
26 }{
27 {"1.2.3.4", "1.2.3.4"},
28 {" 1.2.3.4 ", "1.2.3.4"},
29 {"1.2.3.4.5", ""},
30 {"1.2.3.4:8080", "1.2.3.4"},
31 {"fe80::2", "fe80::2"},
32 {"[fe80::2]:3213", "fe80::2"},
33 {"fe80::2:43214", ""},
34 }
35
36 for i, c := range cases {
37 r := http.Request{
38 RemoteAddr: c.in,
39 }
40 res := remoteIP(&r)
41 got := ""
42 if res != nil {
43 got = res.String()
44 }
45
46 if c.out != got {
47 t.Errorf("Test case %d: got %q, want %q", i, got, c.out)
48 }
49 }
50}