|
- package capstan_test
-
- import (
- "testing"
-
- "git.destrealm.org/go/capstan"
- "git.destrealm.org/go/capstan/config"
- )
-
- func Test_URLMapper_For(t *testing.T) {
- var err error
- var out string
- var builder *capstan.URLBuilder
-
- mapper := capstan.NewURLMapper(&config.ServerConfig{
- FullHost: "localhost",
- Protocol: "http",
- })
- mapper.Map(&capstan.Route{
- Name: "index",
- Path: "/",
- Method: "GET",
- Endpoint: func(ctx capstan.Context) error { return nil },
- })
- mapper.Map(&capstan.Route{
- Name: "test",
- Path: "/test/{value}",
- Method: "GET",
- Endpoint: func(ctx capstan.Context) error { return nil },
- })
-
- builder = mapper.For("index:get")
- if builder == nil {
- t.Error("URLBUilder should be returned from .For(); got nil")
- return
- }
-
- builder.External = true
- out = builder.Encode()
- if err != nil {
- t.Error("URLBuilder.Encode() failed:", err)
- return
- }
- if out != "http://localhost/" {
- t.Error("URL did not match")
- t.Error(`expected: "http://localhost/"`)
- t.Errorf(`actual: "%s"`, out)
- }
-
- builder = mapper.For("test:get")
- if builder == nil {
- t.Error("URLBUilder should be returned from .For(); got nil")
- return
- }
-
- builder = mapper.For("index:get")
- if builder == nil {
- t.Error("URLBUilder should be returned from .For(); got nil")
- return
- }
-
- builder.External = false
- out = builder.Encode()
- if err != nil {
- t.Error("URLBuilder.Encode() failed:", err)
- return
- }
- if out != "/" {
- t.Error("URL did not match")
- t.Error(`expected: "/"`)
- t.Errorf(`actual: "%s"`, out)
- }
-
- builder = mapper.For("test:get")
- if builder == nil {
- t.Error("URLBUilder should be returned from .For(); got nil")
- return
- }
-
- builder = builder.Param("value", "unit-test")
- builder.External = true
- out = builder.Encode()
- if err != nil {
- t.Error("URLBuilder.Encode() failed:", err)
- return
- }
- if out != "http://localhost/test/unit-test" {
- t.Error("URL did not match")
- t.Error(`expected: "http://localhost/test/unit-test"`)
- t.Errorf(`actual: "%s"`, out)
- }
-
- builder = mapper.For("test:get")
- if builder == nil {
- t.Error("URLBUilder should be returned from .For(); got nil")
- return
- }
-
- builder = builder.Param("value", "unit-test")
- builder.External = false
- out = builder.Encode()
- if err != nil {
- t.Error("URLBuilder.Encode() failed:", err)
- return
- }
- if out != "/test/unit-test" {
- t.Error("URL did not match")
- t.Error(`expected: "/test/unit-test"`)
- t.Errorf(`actual: "%s"`, out)
- }
-
- builder = mapper.For("test:get")
- if builder == nil {
- t.Error("URLBUilder should be returned from .For(); got nil")
- return
- }
-
- builder = builder.Param("value", "unit-test")
- builder = builder.Param("query", "string-value")
- builder.External = true
- out = builder.Encode()
- if err != nil {
- t.Error("URLBuilder.Encode() failed:", err)
- return
- }
- if out != "http://localhost/test/unit-test?query=string-value" {
- t.Error("URL did not match")
- t.Error(`expected: "http://localhost/test/unit-test?query=string-value"`)
- t.Errorf(`actual: "%s"`, out)
- }
- }
-
- func Test_URLMapper_ForStatic(t *testing.T) {
- var err error
- var out string
- var builder *capstan.URLBuilder
-
- mapper := capstan.NewURLMapper(&config.ServerConfig{
- FullHost: "localhost",
- Protocol: "http",
- })
- mapper.Static = "/static/"
-
- builder = mapper.For("static")
- if builder == nil {
- t.Error("URLBUilder should be returned from .For(); got nil")
- return
- }
-
- builder.Asset("sample.jpg")
- builder.External = true
- out = builder.Encode()
- if err != nil {
- t.Error("URLBuilder.Encode() failed:", err)
- return
- }
- if out != "http://localhost/static/sample.jpg" {
- t.Error("URL did not match")
- t.Error(`expected: "http://localhost/static/sample.jpg"`)
- t.Errorf(`actual: "%s"`, out)
- }
- }
|