2019-06-21 18:43:25 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-06-17 21:03:18 +02:00
|
|
|
"os"
|
2019-06-21 18:43:25 +02:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2020-07-24 12:03:49 +02:00
|
|
|
"github.com/kataras/iris/v12"
|
2019-10-25 00:27:02 +02:00
|
|
|
"github.com/kataras/iris/v12/httptest"
|
2019-06-21 18:43:25 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type resource string
|
|
|
|
|
|
|
|
func (r resource) contentType() string {
|
|
|
|
switch filepath.Ext(r.String()) {
|
|
|
|
case ".js":
|
2020-04-27 11:28:30 +02:00
|
|
|
return "text/javascript"
|
2019-06-21 18:43:25 +02:00
|
|
|
case ".css":
|
|
|
|
return "text/css"
|
|
|
|
case ".ico":
|
|
|
|
return "image/x-icon"
|
|
|
|
case ".html", "":
|
|
|
|
return "text/html"
|
|
|
|
default:
|
|
|
|
return "text/plain"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r resource) String() string {
|
|
|
|
return string(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r resource) strip(strip string) string {
|
|
|
|
s := r.String()
|
|
|
|
return strings.TrimPrefix(s, strip)
|
|
|
|
}
|
|
|
|
|
2020-04-29 20:16:43 +02:00
|
|
|
func (r resource) loadFromBase(dir string, strip string) string {
|
2019-06-21 18:43:25 +02:00
|
|
|
filename := r.String()
|
|
|
|
|
2020-04-29 20:16:43 +02:00
|
|
|
filename = r.strip(strip)
|
2019-06-21 18:43:25 +02:00
|
|
|
if filepath.Ext(filename) == "" {
|
|
|
|
// root /.
|
|
|
|
filename = filename + "/index.html"
|
|
|
|
}
|
|
|
|
|
|
|
|
fullpath := filepath.Join(dir, filename)
|
|
|
|
|
2022-06-17 21:03:18 +02:00
|
|
|
b, err := os.ReadFile(fullpath)
|
2019-06-21 18:43:25 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(fullpath + " failed with error: " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
result := string(b)
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFileServerBasic(t *testing.T) {
|
2019-08-17 09:06:20 +02:00
|
|
|
urls := []resource{
|
2020-04-29 20:16:43 +02:00
|
|
|
"/v1/static/css/main.css",
|
2020-07-15 10:18:40 +02:00
|
|
|
"/v1/static/js/main.js",
|
2020-04-29 20:16:43 +02:00
|
|
|
"/v1/static/favicon.ico",
|
|
|
|
"/v1/static/app2",
|
|
|
|
"/v1/static/app2/app2app3",
|
|
|
|
"/v1/static",
|
2019-06-21 18:43:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
app := newApp()
|
|
|
|
// route := app.GetRouteReadOnly("GET/{file:path}")
|
|
|
|
// if route == nil {
|
|
|
|
// app.Logger().Fatalf("expected a route to serve files")
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if expected, got := "./assets", route.StaticDir(); expected != got {
|
|
|
|
// app.Logger().Fatalf("expected route's static directory to be: '%s' but got: '%s'", expected, got)
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if !route.StaticDirContainsIndex() {
|
|
|
|
// app.Logger().Fatalf("epxected ./assets to contain an %s file", "/index.html")
|
|
|
|
// }
|
|
|
|
|
|
|
|
e := httptest.New(t, app)
|
|
|
|
for _, u := range urls {
|
|
|
|
url := u.String()
|
2020-04-29 20:16:43 +02:00
|
|
|
contents := u.loadFromBase("./assets", "/v1/static")
|
|
|
|
|
|
|
|
e.GET(url).Expect().
|
|
|
|
Status(httptest.StatusOK).
|
|
|
|
ContentType(u.contentType(), app.ConfigurationReadOnly().GetCharset()).
|
|
|
|
Body().Equal(contents)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests subdomain + request path and system directory with a name that contains a dot(.)
|
|
|
|
func TestHandleDirDot(t *testing.T) {
|
|
|
|
urls := []resource{
|
|
|
|
"/v1/assets.system/css/main.css",
|
|
|
|
}
|
|
|
|
app := newApp()
|
2020-07-24 12:03:49 +02:00
|
|
|
app.Subdomain("test").Party("/v1").HandleDir("/assets.system", iris.Dir("./assets.system"))
|
2020-04-29 20:16:43 +02:00
|
|
|
|
|
|
|
e := httptest.New(t, app, httptest.URL("http://test.example.com"))
|
|
|
|
for _, u := range urls {
|
|
|
|
url := u.String()
|
|
|
|
contents := u.loadFromBase("./assets.system", "/v1/assets.system")
|
2019-06-21 18:43:25 +02:00
|
|
|
|
2020-04-29 20:16:43 +02:00
|
|
|
e.GET(url).Expect().
|
2019-06-21 18:43:25 +02:00
|
|
|
Status(httptest.StatusOK).
|
|
|
|
ContentType(u.contentType(), app.ConfigurationReadOnly().GetCharset()).
|
|
|
|
Body().Equal(contents)
|
|
|
|
}
|
|
|
|
}
|