File: xrds_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 (87 lines) | stat: -rw-r--r-- 2,647 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package openid

import (
	"testing"
)

func TestXrds(t *testing.T) {
	testExpectOpID(t, []byte(`
<?xml version="1.0" encoding="UTF-8"?>
<xrds:XRDS xmlns:xrds="xri://$xrds" xmlns="xri://$xrd*($v*2.0)"
xmlns:openid="http://openid.net/xmlns/1.0">
  <XRD>
    <Service priority="10">
      <Type>http://openid.net/signon/1.0</Type>
      <URI>http://www.myopenid.com/server</URI>
      <openid:Delegate>http://smoker.myopenid.com/</openid:Delegate>
    </Service>
    <Service priority="50">
      <Type>http://openid.net/signon/1.0</Type>
      <URI>http://www.livejournal.com/openid/server.bml</URI>
      <openid:Delegate>
        http://www.livejournal.com/users/frank/
      </openid:Delegate>
    </Service>
    <Service priority="20">
      <Type>http://lid.netmesh.org/sso/2.0</Type>
    </Service>
    <Service>
      <Type>http://specs.openid.net/auth/2.0/server</Type>
      <URI>foo</URI>
    </Service>
  </XRD>
</xrds:XRDS>
    `), "foo", "")

	testExpectOpID(t, []byte(`
<?xml version="1.0" encoding="UTF-8"?>
<xrds:XRDS xmlns:xrds="xri://$xrds" xmlns="xri://$xrd*($v*2.0)"
xmlns:openid="http://openid.net/xmlns/1.0">
  <XRD>
    <Service xmlns="xri://$xrd*($v*2.0)">
      <Type>http://specs.openid.net/auth/2.0/signon</Type>
      <URI>https://www.exampleprovider.com/endpoint/</URI>
      <LocalID>https://exampleuser.exampleprovider.com/</LocalID>
    </Service>
  </XRD>
</xrds:XRDS>
    `),
		"https://www.exampleprovider.com/endpoint/",
		"https://exampleuser.exampleprovider.com/")

	// OP Identifier Element has priority over Claimed Identifier Element
	testExpectOpID(t, []byte(`
<?xml version="1.0" encoding="UTF-8"?>
<xrds:XRDS xmlns:xrds="xri://$xrds" xmlns="xri://$xrd*($v*2.0)"
xmlns:openid="http://openid.net/xmlns/1.0">
  <XRD>
    <Service xmlns="xri://$xrd*($v*2.0)">
      <Type>http://specs.openid.net/auth/2.0/signon</Type>
      <URI>https://www.exampleprovider.com/endpoint-signon/</URI>
    </Service>
    <Service xmlns="xri://$xrd*($v*2.0)">
      <Type>http://specs.openid.net/auth/2.0/server</Type>
      <URI>https://www.exampleprovider.com/endpoint-server/</URI>
    </Service>
  </XRD>
</xrds:XRDS>
    `),
		"https://www.exampleprovider.com/endpoint-server/",
		"")
}

func testExpectOpID(t *testing.T, xrds []byte, op, id string) {
	receivedOp, receivedID, err := parseXrds(xrds)
	if err != nil {
		t.Errorf("Got an error parsing XRDS (%s): %s", string(xrds), err)
	} else {
		if receivedOp != op {
			t.Errorf("Extracted OP does not match: Exepect %s, Got %s",
				op, receivedOp)
		}
		if receivedID != id {
			t.Errorf("Extracted ID does not match: Exepect %s, Got %s",
				id, receivedID)
		}
	}
}