|
| 1 | +/* |
| 2 | + * Copyright (c) 2017, Joyent, Inc. All rights reserved. |
| 3 | + * |
| 4 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 5 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 7 | + */ |
| 8 | +package com.joyent.manta.http; |
| 9 | + |
| 10 | +import org.apache.commons.io.FileUtils; |
| 11 | +import org.apache.http.Header; |
| 12 | +import org.apache.http.HttpHeaders; |
| 13 | +import org.apache.http.entity.ContentType; |
| 14 | +import org.apache.http.entity.ContentType; |
| 15 | +import org.apache.http.message.BasicHeader; |
| 16 | +import org.testng.Assert; |
| 17 | +import org.testng.annotations.Test; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | + |
| 21 | +@Test |
| 22 | +public class ContentTypeLookupTest { |
| 23 | + private static final Header[] EXAMPLE_HEADERS = new Header[] { |
| 24 | + new BasicHeader("Last-Modified","Fri, 09 Dec 2016 22:09:44 GMT"), |
| 25 | + new BasicHeader("Result-Set-Size", "0"), |
| 26 | + new BasicHeader("Date", "Fri, 09 Dec 2016 22:09:44 GMT"), |
| 27 | + new BasicHeader("Server", "Manta"), |
| 28 | + new BasicHeader("x-request-id", "17d243b4-f9a7-4042-bc21-7322fb40fc1c"), |
| 29 | + new BasicHeader("x-response-time", "16"), |
| 30 | + new BasicHeader("x-server-name", "02d02889-cd80-4ac1-bc0c-4775b86661e4"), |
| 31 | + new BasicHeader("Connection", "keep-alive")}; |
| 32 | + |
| 33 | + public void canFindDefault() { |
| 34 | + MantaHttpHeaders headers = new MantaHttpHeaders(EXAMPLE_HEADERS); |
| 35 | + ContentType troff = ContentType.create("application/x-troff"); |
| 36 | + ContentType jsonStream = ContentType.create("application/x-json-stream"); |
| 37 | + |
| 38 | + Assert.assertEquals(ContentTypeLookup.findOrDefaultContentType(null, troff).getMimeType(), |
| 39 | + troff.getMimeType()); |
| 40 | + Assert.assertEquals(ContentTypeLookup.findOrDefaultContentType(headers, troff).getMimeType(), |
| 41 | + troff.getMimeType()); |
| 42 | + headers.put("Content-Type", "application/x-json-stream; type=directory"); |
| 43 | + Assert.assertEquals(ContentTypeLookup.findOrDefaultContentType(headers, troff).getMimeType(), |
| 44 | + jsonStream.getMimeType()); |
| 45 | + } |
| 46 | + |
| 47 | + // These test will fail on some platforms where libgio will return |
| 48 | + // "text/plain" for empty files instead of indicating an unknown type. |
| 49 | + @Test(enabled = false) |
| 50 | + public void canfindByMultipleMethodsNull() throws Exception { |
| 51 | + MantaHttpHeaders headers = new MantaHttpHeaders(EXAMPLE_HEADERS); |
| 52 | + ContentType troff = ContentType.create("application/x-troff"); |
| 53 | + ContentType jsonStream = ContentType.create("application/x-json-stream"); |
| 54 | + |
| 55 | + File temp = File.createTempFile("upload", ".unknown"); |
| 56 | + FileUtils.forceDeleteOnExit(temp); |
| 57 | + Assert.assertNull(ContentTypeLookup.findOrDefaultContentType(null, "/stor/unknown", temp, null)); |
| 58 | + |
| 59 | + temp = File.createTempFile("upload", ".unknown"); |
| 60 | + FileUtils.forceDeleteOnExit(temp); |
| 61 | + Assert.assertNull(ContentTypeLookup.findOrDefaultContentType(headers, "/stor/unknown", temp, null)); |
| 62 | + } |
| 63 | + |
| 64 | + public void canfindByMultipleMethods() throws Exception { |
| 65 | + MantaHttpHeaders headers = new MantaHttpHeaders(EXAMPLE_HEADERS); |
| 66 | + ContentType troff = ContentType.create("application/x-troff"); |
| 67 | + ContentType jsonStream = ContentType.create("application/x-json-stream"); |
| 68 | + |
| 69 | + File temp = File.createTempFile("upload", ".jpeg"); |
| 70 | + FileUtils.forceDeleteOnExit(temp); |
| 71 | + Assert.assertEquals(ContentTypeLookup.findOrDefaultContentType(headers, |
| 72 | + "/stor/unknown", |
| 73 | + temp, |
| 74 | + troff).getMimeType(), |
| 75 | + "image/jpeg"); |
| 76 | + headers.put("Content-Type", "application/x-json-stream; type=directory"); |
| 77 | + temp = File.createTempFile("upload", ".jpeg"); |
| 78 | + FileUtils.forceDeleteOnExit(temp); |
| 79 | + Assert.assertEquals(ContentTypeLookup.findOrDefaultContentType(headers, |
| 80 | + "/stor/unknown", |
| 81 | + temp, |
| 82 | + troff).getMimeType(), |
| 83 | + jsonStream.getMimeType()); |
| 84 | + } |
| 85 | + |
| 86 | + public void canFindByFilename() { |
| 87 | + MantaHttpHeaders headers = new MantaHttpHeaders(EXAMPLE_HEADERS); |
| 88 | + ContentType troff = ContentType.create("application/x-troff"); |
| 89 | + ContentType jsonStream = ContentType.create("application/x-json-stream"); |
| 90 | + |
| 91 | + Assert.assertNull(ContentTypeLookup.findOrDefaultContentType(null, "/tmp/unknown", null)); |
| 92 | + Assert.assertNull(ContentTypeLookup.findOrDefaultContentType(headers, "/tmp/unknown", null)); |
| 93 | + |
| 94 | + Assert.assertEquals(ContentTypeLookup.findOrDefaultContentType(headers, |
| 95 | + "/tmp/unknown", |
| 96 | + troff).getMimeType(), |
| 97 | + troff.getMimeType()); |
| 98 | + |
| 99 | + Assert.assertEquals(ContentTypeLookup.findOrDefaultContentType(headers, |
| 100 | + "/tmp/foo.jpeg", |
| 101 | + troff).getMimeType(), |
| 102 | + "image/jpeg"); |
| 103 | + headers.put("Content-Type", "application/x-json-stream; type=directory"); |
| 104 | + Assert.assertEquals(ContentTypeLookup.findOrDefaultContentType(headers, |
| 105 | + "/tmp/foo.jpeg", |
| 106 | + troff).getMimeType(), |
| 107 | + jsonStream.getMimeType()); |
| 108 | + |
| 109 | + } |
| 110 | +} |
0 commit comments