Browse Source

- expose Host for (sub)domains.

tags/v0.9.0
jimzhan 8 years ago
parent
commit
6f54e8476a
2 changed files with 34 additions and 0 deletions
  1. +11
    -0
      server.go
  2. +23
    -0
      server_test.go

+ 11
- 0
server.go View File

return server return server
} }


// Host creates a new application group under the given (sub)domain.
func (self *server) Host(domain string) *server {
var middleware = new(middleware)
self.mux.Host(domain).Handler(middleware)
var mux = self.mux.Host(domain).Subrouter()

server := &server{middleware: middleware, mux: mux}
self.subservers = append(self.subservers, server)
return server
}

// Name returns route name for the given request, if any. // Name returns route name for the given request, if any.
func (self *server) Name(r *http.Request) (name string) { func (self *server) Name(r *http.Request) (name string) {
var match mux.RouteMatch var match mux.RouteMatch

+ 23
- 0
server_test.go View File

}) })
} }


func TestHost(t *testing.T) {
app := New()
app.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Powered-By", "base")
})

user := app.Host("localhost")
user.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Powered-By", "rex")
})

Convey("rex.Host", t, func() {
request, _ := http.NewRequest("GET", "http://localhost/", nil)
response := httptest.NewRecorder()

app.ServeHTTP(response, request)
So(response.Header().Get("X-Powered-By"), ShouldEqual, "base")

user.ServeHTTP(response, request)
So(response.Header().Get("X-Powered-By"), ShouldEqual, "rex")
})
}

func TestFileServer(t *testing.T) { func TestFileServer(t *testing.T) {
Convey("rex.FileServer", t, func() { Convey("rex.FileServer", t, func() {
var ( var (

Loading…
Cancel
Save