~samwhited/xmpp

f159edd01b1d0a3a735baf51441b28593536d249 — Sam Whited 5 years ago f2b5a0e
internal: Add host-meta XRD struct
2 files changed, 66 insertions(+), 0 deletions(-)

A internal/lookup.go
A internal/lookup_test.go
A internal/lookup.go => internal/lookup.go +30 -0
@@ 0,0 1,30 @@
// Copyright 2016 Sam Whited.
// Use of this source code is governed by the BSD 2-clause license that can be
// found in the LICENSE file.

package internal

import (
	"encoding/xml"
)

// Represents a document of the form:
//
// <?xml version='1.0' encoding=utf-9'?>
// <XRD xmlns='http://docs.oasis-open.org/ns/xri/xrd-1.0'>
//   …
//   <Link rel="urn:xmpp:alt-connections:xbosh"
//         href="https://web.example.com:5280/bosh" />
//   <Link rel="urn:xmpp:alt-connections:websocket"
//         href="wss://web.example.com:443/ws" />
//   …
// </XRD>
type XRD struct {
	XMLName xml.Name `xml:"http://docs.oasis-open.org/ns/xri/xrd-1.0 XRD"`
	Links   []Link   `xml:"Link"`
}

type Link struct {
	Rel  string `xml:"rel,attr"`
	Href string `xml:"href,attr"`
}

A internal/lookup_test.go => internal/lookup_test.go +36 -0
@@ 0,0 1,36 @@
// Copyright 2016 Sam Whited.
// Use of this source code is governed by the BSD 2-clause license that can be
// found in the LICENSE file.

package internal

import (
	"encoding/xml"
	"testing"
)

var (
	boshLink = Link{Rel: "urn:xmpp:alt-connections:xbosh", Href: "https://web.example.com:5280/bosh"}
	wsLink   = Link{Rel: "urn:xmpp:alt-connections:websocket", Href: "wss://web.example.com:443/ws"}
)

func TestUnmarshalWellKnown(t *testing.T) {
	hostMeta := []byte(`<XRD xmlns='http://docs.oasis-open.org/ns/xri/xrd-1.0'>
  <Link rel="urn:xmpp:alt-connections:xbosh"
        href="https://web.example.com:5280/bosh" />
  <Link rel="urn:xmpp:alt-connections:websocket"
        href="wss://web.example.com:443/ws" />
</XRD>`)
	var xrd XRD
	if err := xml.Unmarshal(hostMeta, &xrd); err != nil {
		t.Fatal(err)
	}
	switch {
	case len(xrd.Links) != 2:
		t.Fatalf("internal: Expected 2 links in xrd unmarshal output, but found %d", len(xrd.Links))
	case xrd.Links[0] != boshLink:
		t.Fatalf("internal: Expected %v, but got %v", boshLink, xrd.Links[0])
	case xrd.Links[1] != wsLink:
		t.Fatalf("internal: Expected %v, but got %v", wsLink, xrd.Links[1])
	}
}