File: html_discovery_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 (62 lines) | stat: -rw-r--r-- 1,508 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
package openid

import (
	"bytes"
	"testing"
)

func TestFindEndpointFromLink(t *testing.T) {
	searchLink(t, `
      <html>
        <head>
          <link rel="openid2.provider" href="example.com/openid">
      `, "example.com/openid", "", false)
	searchLink(t, `
      <html>
        <head>
          <link rel="openid2.provider" href="foo.com">
          <link rel="openid2.local_id" href="bar-name">
        </head>
      </html>
      `, "foo.com", "bar-name", false)
	// Self-closing link
	searchLink(t, `
      <html>
        <head>
          <link rel="openid2.provider" href="selfclose.com" />
          <link rel="openid2.local_id" href="selfclose-name" />
        </head>
      </html>
      `, "selfclose.com", "selfclose-name", false)
}

func TestNoEndpointFromLink(t *testing.T) {
	searchLink(t, `
      <html>
        <head>
          <link rel="openid2.provider">
      `, "", "", true)
	// Outside of head.
	searchLink(t, `
      <html>
        <head></head>
        <link rel="openid2.provider" href="example.com/openid">
      `, "", "", true)
}

func searchLink(t *testing.T, doc, opEndpoint, claimedID string, err bool) {
	r := bytes.NewReader([]byte(doc))
	op, id, e := findProviderFromHeadLink(r)
	if (e != nil) != err {
		t.Errorf("Unexpected error: '%s'", e)
	} else if e == nil {
		if op != opEndpoint {
			t.Errorf("Found bad endpoint: Expected %s, Got %s",
				op, opEndpoint)
		}
		if id != claimedID {
			t.Errorf("Found bad id: Expected %s, Got %s",
				id, claimedID)
		}
	}
}