aboutsummaryrefslogtreecommitdiffstats
path: root/swarm/api/api_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'swarm/api/api_test.go')
-rw-r--r--swarm/api/api_test.go67
1 files changed, 67 insertions, 0 deletions
diff --git a/swarm/api/api_test.go b/swarm/api/api_test.go
index a65bf07e2..eb896f32a 100644
--- a/swarm/api/api_test.go
+++ b/swarm/api/api_test.go
@@ -17,6 +17,7 @@
package api
import (
+ "bytes"
"context"
"errors"
"flag"
@@ -433,3 +434,69 @@ func TestDecryptOrigin(t *testing.T) {
}
}
}
+
+func TestDetectContentType(t *testing.T) {
+ for _, tc := range []struct {
+ file string
+ content string
+ expectedContentType string
+ }{
+ {
+ file: "file-with-correct-css.css",
+ content: "body {background-color: orange}",
+ expectedContentType: "text/css; charset=utf-8",
+ },
+ {
+ file: "empty-file.css",
+ content: "",
+ expectedContentType: "text/css; charset=utf-8",
+ },
+ {
+ file: "empty-file.pdf",
+ content: "",
+ expectedContentType: "application/pdf",
+ },
+ {
+ file: "empty-file.md",
+ content: "",
+ expectedContentType: "text/markdown; charset=utf-8",
+ },
+ {
+ file: "empty-file-with-unknown-content.strangeext",
+ content: "",
+ expectedContentType: "text/plain; charset=utf-8",
+ },
+ {
+ file: "file-with-unknown-extension-and-content.strangeext",
+ content: "Lorem Ipsum",
+ expectedContentType: "text/plain; charset=utf-8",
+ },
+ {
+ file: "file-no-extension",
+ content: "Lorem Ipsum",
+ expectedContentType: "text/plain; charset=utf-8",
+ },
+ {
+ file: "file-no-extension-no-content",
+ content: "",
+ expectedContentType: "text/plain; charset=utf-8",
+ },
+ {
+ file: "css-file-with-html-inside.css",
+ content: "<!doctype html><html><head></head><body></body></html>",
+ expectedContentType: "text/css; charset=utf-8",
+ },
+ } {
+ t.Run(tc.file, func(t *testing.T) {
+ detected, err := DetectContentType(tc.file, bytes.NewReader([]byte(tc.content)))
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ if detected != tc.expectedContentType {
+ t.Fatalf("File: %s, Expected mime type %s, got %s", tc.file, tc.expectedContentType, detected)
+ }
+
+ })
+ }
+}