Kaynağa Gözat

- expose Host for (sub)domains.

tags/v0.9.0
jimzhan 8 yıl önce
ebeveyn
işleme
6f54e8476a
2 değiştirilmiş dosya ile 34 ekleme ve 0 silme
  1. +11
    -0
      server.go
  2. +23
    -0
      server_test.go

+ 11
- 0
server.go Dosyayı Görüntüle

@@ -102,6 +102,17 @@ func (self *server) Group(prefix string) *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.
func (self *server) Name(r *http.Request) (name string) {
var match mux.RouteMatch

+ 23
- 0
server_test.go Dosyayı Görüntüle

@@ -288,6 +288,29 @@ func TestGroup(t *testing.T) {
})
}

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) {
Convey("rex.FileServer", t, func() {
var (

Yükleniyor…
İptal
Kaydet