File: normalizer_test.go

package info (click to toggle)
golang-github-yohcop-openid-go 0.0~git20170901.0.cfc72ed-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 184 kB
  • sloc: makefile: 3
file content (54 lines) | stat: -rw-r--r-- 2,112 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package openid

import (
	"testing"
)

func TestNormalize(t *testing.T) {
	// OpenID 2.0 spec Appendix A.1. Normalization
	doNormalize(t, "example.com", "http://example.com/", true)
	doNormalize(t, "http://example.com", "http://example.com/", true)
	doNormalize(t, "https://example.com/", "https://example.com/", true)
	doNormalize(t, "http://example.com/user", "http://example.com/user", true)
	doNormalize(t, "http://example.com/user/", "http://example.com/user/", true)
	doNormalize(t, "http://example.com/", "http://example.com/", true)
	doNormalize(t, "=example", "=example", false)       // XRI not supported
	doNormalize(t, "(=example)", "(=example)", false)   // XRI not supported
	doNormalize(t, "xri://=example", "=example", false) // XRI not supported

	// Empty
	doNormalize(t, "", "", false)
	doNormalize(t, " ", "", false)
	doNormalize(t, "	", "", false)
	doNormalize(t, "xri://", "", false)
	doNormalize(t, "http://", "", false)
	doNormalize(t, "https://", "", false)

	// Padded with spacing
	doNormalize(t, " example.com  ", "http://example.com/", true)
	doNormalize(t, " 	http://example.com		 ", "http://example.com/", true)

	// XRI not supported
	doNormalize(t, "xri://asdf", "asdf", false)
	doNormalize(t, "=asdf", "=asdf", false)
	doNormalize(t, "@asdf", "@asdf", false)

	// HTTP
	doNormalize(t, "foo.com", "http://foo.com/", true)
	doNormalize(t, "http://foo.com", "http://foo.com/", true)
	doNormalize(t, "https://foo.com", "https://foo.com/", true)

	// Fragment need to be removed
	doNormalize(t, "http://foo.com#bar", "http://foo.com/", true)
	doNormalize(t, "http://foo.com/page#bar", "http://foo.com/page", true)
}

func doNormalize(t *testing.T, idIn, idOut string, succeed bool) {
	if id, err := Normalize(idIn); err != nil && succeed {
		t.Errorf("unexpected normalize error: gave %v, expected %v, got %v - %v", idIn, idOut, id, err)
	} else if err == nil && !succeed {
		t.Errorf("unexpected normalize success: gave %v, expected %v, got %v", idIn, idOut, id)
	} else if id != idOut {
		t.Errorf("unexpected normalize result: gave %v, expected %v, got %v", idIn, idOut, id)
	}
}