package wrouter import ( "net/http" "net/http/httptest" "strings" "testing" ) func TestStatic(t *testing.T) { router := &Router{} router.Static("/data", "./") server := httptest.NewServer(router) t.Run("Download", func(t *testing.T) { resp, err := http.Get(server.URL + "/data/README.md") if err != nil { t.Error("Request:", err) t.Fail() } if resp.StatusCode != 200 { t.Error("Expected 200, got", resp.Status) t.Fail() } if resp.ContentLength == 0 { t.Error("No content returned from server") t.Fail() } if !strings.Contains(resp.Header.Get("Content-Type"), "text/plain") { t.Errorf("Content-Type %s != %s", resp.Header.Get("Content-Type"), "text/plain") t.Fail() } }) t.Run("Download_Fail", func(t *testing.T) { resp, err := http.Get(server.URL + "/data/f42klfk2kf2kfk.md") if err != nil { t.Error("Request:", err) t.Fail() } if resp.StatusCode != 404 { t.Error("Expected 404, got", resp.Status) t.Fail() } }) t.Run("Download_Fail2", func(t *testing.T) { resp, err := http.Get(server.URL + "/data/../../../../../../../etc/passwd") if err != nil { t.Error("Request:", err) t.Fail() } if resp.StatusCode != 403 { t.Error("Expected 403, got", resp.Status) t.Fail() } }) }