Преглед на файлове

mw: static tests

tags/v0.9.0
jimzhan преди 9 години
родител
ревизия
24fb2af48e
променени са 3 файла, в които са добавени 64 реда и са изтрити 1 реда
  1. +29
    -0
      middleware/compress_test.go
  2. +1
    -1
      middleware/static.go
  3. +34
    -0
      middleware/static_test.go

+ 29
- 0
middleware/compress_test.go Целия файл

@@ -1 +1,30 @@
package middleware

import (
"io"
"net/http"
"net/http/httptest"
"testing"

"github.com/goanywhere/rex"
. "github.com/smartystreets/goconvey/convey"
)

func TestCompress(t *testing.T) {

app := rex.New()
app.Use(Compress)
app.GET("/", func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "app")
})

Convey("rex.middleware.Compress", t, func() {
request, _ := http.NewRequest("GET", "/", nil)
request.Header.Set("Accept-Encoding", "gzip")
response := httptest.NewRecorder()

app.ServeHTTP(response, request)

So(response.Header().Get("Content-Encoding"), ShouldEqual, "gzip")
})
}

+ 1
- 1
middleware/static.go Целия файл

@@ -11,7 +11,7 @@ import (
func Static(dir string) func(http.Handler) http.Handler {
var (
fs = http.Dir(dir)
prefix = path.Join("/", dir)
prefix = path.Join("/", path.Base(path.Dir(dir)))
)
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

+ 34
- 0
middleware/static_test.go Целия файл

@@ -0,0 +1,34 @@
package middleware

import (
"io"
"net/http"
"net/http/httptest"
"os"
"path"
"testing"

"github.com/goanywhere/rex"
. "github.com/smartystreets/goconvey/convey"
)

func TestStatic(t *testing.T) {
tempdir := os.TempDir()
filename := path.Join(tempdir, "favicon.ico")

app := rex.New()
app.Use(Static(tempdir))
prefix := path.Join("/", path.Base(path.Dir(filename)))
app.GET(prefix, func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "app")
})

Convey("rex.middleware.Static", t, func() {
request, _ := http.NewRequest("GET", path.Join(prefix, filename), nil)
response := httptest.NewRecorder()

app.ServeHTTP(response, request)

So(response.Code, ShouldEqual, http.StatusOK)
})
}

Loading…
Отказ
Запис