MCPcopy Create free account
hub / github.com/crewjam/saml / assertXHTMLDocument

Function assertXHTMLDocument

xhtml_forms_test.go:28–66  ·  view source on GitHub ↗

assertXHTMLDocument checks that page is a well-formed XML document whose root is an XHTML html element holding a head with a title and a body, as saml-bindings-2.0-os §3.5.4 requires of the pages that carry a SAML message with the HTTP-POST binding ("The HTML document MUST adhere to the XHTML specif

(t *testing.T, page []byte)

Source from the content-addressed store, hash-verified

26// message with the HTTP-POST binding ("The HTML document MUST adhere to the
27// XHTML specification").
28func assertXHTMLDocument(t *testing.T, page []byte) {
29 t.Helper()
30 d := xml.NewDecoder(bytes.NewReader(page))
31 d.Strict = true
32
33 var path []xml.Name
34 roots := 0
35 seen := map[string]bool{}
36 for {
37 tok, err := d.Token()
38 if errors.Is(err, io.EOF) {
39 break
40 }
41 assert.NilError(t, err, "page is not well-formed XML:\n%s", page)
42 switch tok := tok.(type) {
43 case xml.StartElement:
44 if len(path) == 0 {
45 roots++
46 assert.Check(t, is.Equal(xml.Name{Space: xhtmlNamespace, Local: "html"}, tok.Name))
47 }
48 assert.Check(t, is.Equal(xhtmlNamespace, tok.Name.Space), "element %q is not in the XHTML namespace", tok.Name.Local)
49 path = append(path, tok.Name)
50 names := make([]string, 0, len(path))
51 for _, n := range path {
52 names = append(names, n.Local)
53 }
54 seen[strings.Join(names, "/")] = true
55 case xml.EndElement:
56 path = path[:len(path)-1]
57 case xml.CharData:
58 if len(path) == 0 {
59 assert.Check(t, is.Equal("", strings.TrimSpace(string(tok))), "text outside the root element")
60 }
61 }
62 }
63 assert.Check(t, is.Equal(1, roots))
64 assert.Check(t, seen["html/head/title"], "page has no html/head/title")
65 assert.Check(t, seen["html/body"], "page has no html/body")
66}
67
68// wrapXHTML puts fragment, a form made by one of the PostWithNonce methods,
69// in a page the way samlsp.Middleware does.

Callers 2

TestXHTMLIdPFormsFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected