package sitemap import ( "context" "io/ioutil" "net/http" "net/http/httptest" "net/url" "testing" ) func TestRegisterRawString(t *testing.T) { s := SiteMap().RegisterRawString("/admin").EncodeToXml(context.TODO()) expected := `/admin` if s != expected { t.Errorf("\n\tExpected value: %s\n \tbut got: %s", expected, s) } } func TestRegisterURL(t *testing.T) { s := SiteMap().RegisterURL(URL{Loc: "/admin"}).EncodeToXml(context.TODO()) expected := `/admin` if s != expected { t.Errorf("\n\tExpected value: %s\n \tbut got: %s", expected, s) } } func TestRegisterURLAndRawString(t *testing.T) { s := SiteMap().RegisterRawString("/admin1").RegisterURL(URL{Loc: "/admin2"}).EncodeToXml(context.TODO()) expected := `/admin1/admin2` if s != expected { t.Errorf("\n\tExpected value: %s\n \tbut got: %s", expected, s) } } func TestRegisterContextFunc(t *testing.T) { s := SiteMap().RegisterContextFunc(func(context.Context) []URL { return []URL{{Loc: "/admin1"}, {Loc: "/admin2"}} }).EncodeToXml(context.TODO()) expected := `/admin1/admin2` if s != expected { t.Errorf("\n\tExpected value: %s\n \tbut got: %s", expected, s) } } type post struct { } func (p post) Sitemap(ctx context.Context) []URL { return []URL{ {Loc: "/post1"}, {Loc: "/post2"}, } } func TestRegisterModel(t *testing.T) { s := SiteMap().RegisterModel(post{}).EncodeToXml(context.TODO()) expected := `/post1/post2` if s != expected { t.Errorf("\n\tExpected value: %s\n \tbut got: %s", expected, s) } } func TestSiteMapIndex(t *testing.T) { s := SiteMapIndex().RegisterSiteMap(SiteMap(), SiteMap("product"), SiteMap("admin")).EncodeToXml(context.TODO()) expected := `/sitemap.xml/product.xml/admin.xml` if s != expected { t.Errorf("\n\tExpected value: %s\n \tbut got: %s", expected, s) } } func TestEncodeToXmlWithContext(t *testing.T) { s := SiteMap().RegisterRawString("/admin", "https://qor5-1.dev.com/product").EncodeToXml(WithHost("https://qor5.dev.com")) expected := `https:/qor5.dev.com/adminhttps://qor5-1.dev.com/product` if s != expected { t.Errorf("\n\tExpected value: %s\n \tbut got: %s", expected, s) } } func TestRequestHost(t *testing.T) { u, _ := url.Parse("https://qor5.dev.com/sitemap.xml") s := EncodeToXmlByRequest(&http.Request{URL: u}, SiteMap().RegisterRawString("/admin", "https://qor5-1.dev.com/product")) expected := `https:/qor5.dev.com/adminhttps://qor5-1.dev.com/product` if s != expected { t.Errorf("\n\tExpected value: %s\n \tbut got: %s", expected, s) } } func TestServeHTTP(t *testing.T) { site := SiteMap().RegisterRawString("/admin", "https://qor5-1.dev.com/product") serveMux := http.NewServeMux() site.MountTo(serveMux) server := httptest.NewServer(serveMux) resp, err := http.Get(server.URL + "/sitemap.xml") if err != nil { t.Error(err) } s, err := ioutil.ReadAll(resp.Body) if err != nil { t.Error(err) } expected := `/adminhttps://qor5-1.dev.com/product` if string(s) != expected { t.Errorf("\n\tExpected value: %s\n \tbut got: %s", expected, s) } }