Browse Source

Fix Content-Type header for empty resources

Daniel Friedman 6 years ago
parent
commit
fc9c78a178
4 changed files with 43 additions and 6 deletions
  1. 5 1
      mime.go
  2. 15 0
      mime_test.go
  3. 2 5
      server.go
  4. 21 0
      server_test.go

+ 5 - 1
mime.go

@@ -107,6 +107,10 @@ func LookupExt(ctype string) string {
 	return ""
 }
 
+func LookUpCtype(ext string) string {
+	return mimeRdfExt[ext]
+}
+
 func AddRDFExtension(ext string) {
 	rdfExtensions = append(rdfExtensions, ext)
 }
@@ -127,7 +131,7 @@ func MimeLookup(path string) (string, string, bool) {
 	if len(ext) > 0 {
 		if IsRdfExtension(ext) {
 			maybeRDF = true
-			mimeType = LookupExt(ext)
+			mimeType = LookUpCtype(ext)
 		} else {
 			mimeType = mime.TypeByExtension(ext)
 			if len(mimeType) > 0 {

+ 15 - 0
mime_test.go

@@ -162,3 +162,18 @@ func TestMapPathToExtension(t *testing.T) {
 	assert.NoError(t, err)
 	assert.Contains(t, res, path+"$.htm")
 }
+
+func TestLookUpCtype(t *testing.T) {
+	cases := []struct {
+		in, want string
+	}{
+		{".ttl", "text/turtle"},
+		{".n3", "text/n3"},
+		{".rdf", "application/rdf+xml"},
+		{".jsonld", "application/ld+json"},
+		{".unrecognized_ext", ""},
+	}
+	for _, c := range cases {
+		assert.Equal(t, c.want, LookUpCtype(c.in))
+	}
+}

+ 2 - 5
server.go

@@ -585,6 +585,7 @@ func (s *Server) handle(w http.ResponseWriter, req *httpRequest) (r *response) {
 			}
 		} else {
 			magicType = resource.FileType
+			maybeRDF = resource.MaybeRDF
 			if len(mimeRdfExt[resource.Extension]) > 0 {
 				maybeRDF = true
 			}
@@ -647,11 +648,7 @@ func (s *Server) handle(w http.ResponseWriter, req *httpRequest) (r *response) {
 
 		if maybeRDF {
 			g.ReadFile(resource.File)
-			if g.Len() == 0 {
-				maybeRDF = false
-			} else {
-				w.Header().Set(HCType, contentType)
-			}
+			w.Header().Set(HCType, contentType)
 		}
 
 		data := ""

+ 21 - 0
server_test.go

@@ -455,6 +455,27 @@ func TestLDPPostLDPRNoSlug(t *testing.T) {
 	assert.Equal(t, 200, response.StatusCode)
 }
 
+func TestLDPGetLDPR(t *testing.T) {
+	resource_url := testServer.URL + "/_test/resource.ttl"
+
+	request, err := http.NewRequest("PUT", resource_url, nil)
+	assert.NoError(t, err)
+	response, err := httpClient.Do(request)
+	assert.NoError(t, err)
+
+	request, err = http.NewRequest("GET", resource_url, nil)
+	assert.NoError(t, err)
+	response, err = httpClient.Do(request)
+	assert.NoError(t, err)
+
+	assert.Equal(t, "text/turtle", response.Header.Get("Content-Type"))
+
+	request, err = http.NewRequest("DELETE", resource_url, nil)
+	assert.NoError(t, err)
+	response, err = httpClient.Do(request)
+	assert.NoError(t, err)
+}
+
 func TestLDPGetLDPC(t *testing.T) {
 	request, err := http.NewRequest("HEAD", testServer.URL+"/_test/", nil)
 	assert.NoError(t, err)