Pārlūkot izejas kodu

mw: static tests

tags/v0.9.0
jimzhan pirms 9 gadiem
vecāks
revīzija
24fb2af48e
3 mainītis faili ar 64 papildinājumiem un 1 dzēšanām
  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 Parādīt failu

@@ -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 Parādīt failu

@@ -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 Parādīt failu

@@ -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)
})
}

Notiek ielāde…
Atcelt
Saglabāt