|
| 1 | +/* |
| 2 | + * Copyright (c) 2020 DuckDuckGo |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.duckduckgo.app.browser.downloader |
| 18 | + |
| 19 | +import org.junit.Assert.assertEquals |
| 20 | +import org.junit.Test |
| 21 | + |
| 22 | +class UriUtilsFilenameExtractorTest { |
| 23 | + |
| 24 | + private val testee: FilenameExtractor = FilenameExtractor() |
| 25 | + |
| 26 | + @Test |
| 27 | + fun whenUrlEndsWithFilenameAsJpgNoMimeOrContentDispositionThenFilenameShouldBeExtracted() { |
| 28 | + val url = "https://example.com/realFilename.jpg" |
| 29 | + val mimeType: String? = null |
| 30 | + val contentDisposition: String? = null |
| 31 | + val extracted = testee.extract(buildPendingDownload(url, contentDisposition, mimeType)) |
| 32 | + assertEquals("realFilename.jpg", extracted) |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + fun whenUrlContainsFilenameButContainsAdditionalPathSegmentsThenFilenameShouldBeExtracted() { |
| 37 | + val url = "https://foo.example.com/path/images/b/b1/realFilename.jpg/other/stuff" |
| 38 | + val mimeType: String? = null |
| 39 | + val contentDisposition: String? = null |
| 40 | + val extracted = testee.extract(buildPendingDownload(url, contentDisposition, mimeType)) |
| 41 | + assertEquals("realFilename.jpg", extracted) |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + fun whenUrlContainsFilenameButContainsAdditionalPathSegmentsAndQueryParamsThenFilenameShouldBeExtracted() { |
| 46 | + val url = "https://foo.example.com/path/images/b/b1/realFilename.jpg/other/stuff?cb=123" |
| 47 | + val mimeType: String? = null |
| 48 | + val contentDisposition: String? = null |
| 49 | + val extracted = testee.extract(buildPendingDownload(url, contentDisposition, mimeType)) |
| 50 | + assertEquals("realFilename.jpg", extracted) |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + fun whenUrlContainsFilenameButContainsAdditionalPathSegmentsAndQueryParamsWhichLookLikeAFilenameThenFilenameShouldBeExtracted() { |
| 55 | + val url = "https://foo.example.com/path/images/b/b1/realFilename.jpg/other/stuff?cb=123.com" |
| 56 | + val mimeType: String? = null |
| 57 | + val contentDisposition: String? = null |
| 58 | + val extracted = testee.extract(buildPendingDownload(url, contentDisposition, mimeType)) |
| 59 | + assertEquals("realFilename.jpg", extracted) |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + fun whenUrlContainsFilenameButContentDispositionSaysOtherwiseThenExtractFromContentDisposition() { |
| 64 | + val url = "https://example.com/filename.jpg" |
| 65 | + val mimeType: String? = null |
| 66 | + val contentDisposition: String? = "Content-Disposition: attachment; filename=fromDisposition.jpg" |
| 67 | + val extracted = testee.extract(buildPendingDownload(url, contentDisposition, mimeType)) |
| 68 | + assertEquals("fromDisposition.jpg", extracted) |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + fun whenFilenameEndsInBinThenThatIsExtracted() { |
| 73 | + val url = "https://example.com/realFilename.bin" |
| 74 | + val mimeType: String? = null |
| 75 | + val contentDisposition: String? = null |
| 76 | + val extracted = testee.extract(buildPendingDownload(url, contentDisposition, mimeType)) |
| 77 | + assertEquals("realFilename.bin", extracted) |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + fun whenUrlContainsNoFileNameButLotsOfPathsSegmentsThenFirstSegmentNameIsUsed() { |
| 82 | + val url = "https://example.com/foo/bar/files" |
| 83 | + val mimeType: String? = null |
| 84 | + val contentDisposition: String? = null |
| 85 | + val extracted = testee.extract(buildPendingDownload(url, contentDisposition, mimeType)) |
| 86 | + assertEquals("foo.bin", extracted) |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + fun whenFilenameEndsInBinWithASlashThenThatIsExtracted() { |
| 91 | + val url = "https://example.com/realFilename.bin/" |
| 92 | + val mimeType: String? = null |
| 93 | + val contentDisposition: String? = null |
| 94 | + val extracted = testee.extract(buildPendingDownload(url, contentDisposition, mimeType)) |
| 95 | + assertEquals("realFilename.bin", extracted) |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + fun whenFilenameContainsBinThenThatIsExtracted() { |
| 100 | + val url = "https://example.com/realFilename.bin/foo/bar" |
| 101 | + val mimeType: String? = null |
| 102 | + val contentDisposition: String? = null |
| 103 | + val extracted = testee.extract(buildPendingDownload(url, contentDisposition, mimeType)) |
| 104 | + assertEquals("realFilename.bin", extracted) |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + fun whenUrlIsEmptyStringAndNoOtherDataProvidedThenDefaultNameAndBinFiletypeReturned() { |
| 109 | + val url = "" |
| 110 | + val mimeType: String? = null |
| 111 | + val contentDisposition: String? = null |
| 112 | + val extracted = testee.extract(buildPendingDownload(url, contentDisposition, mimeType)) |
| 113 | + assertEquals("downloadfile.bin", extracted) |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + fun whenUrlIsEmptyStringAndMimeTypeProvidedThenDefaultNameAndFiletypeFromMimeReturned() { |
| 118 | + val url = "" |
| 119 | + val mimeType: String? = "image/jpeg" |
| 120 | + val contentDisposition: String? = null |
| 121 | + val extracted = testee.extract(buildPendingDownload(url, contentDisposition, mimeType)) |
| 122 | + assertEquals("downloadfile.jpg", extracted) |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + fun whenUrlIsEmptyStringAndContentDispositionProvidedThenExtractFromContentDisposition() { |
| 127 | + val url = "" |
| 128 | + val mimeType: String? = null |
| 129 | + val contentDisposition: String? = "Content-Disposition: attachment; filename=fromDisposition.jpg" |
| 130 | + val extracted = testee.extract(buildPendingDownload(url, contentDisposition, mimeType)) |
| 131 | + assertEquals("fromDisposition.jpg", extracted) |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + fun whenNoFilenameAndNoPathSegmentsThenDomainNameReturned() { |
| 136 | + val url = "http://example.com" |
| 137 | + val mimeType: String? = null |
| 138 | + val contentDisposition: String? = null |
| 139 | + val extracted = testee.extract(buildPendingDownload(url, contentDisposition, mimeType)) |
| 140 | + assertEquals("example.com", extracted) |
| 141 | + } |
| 142 | + |
| 143 | + private fun buildPendingDownload(url: String, contentDisposition: String?, mimeType: String?): FileDownloader.PendingFileDownload { |
| 144 | + return FileDownloader.PendingFileDownload( |
| 145 | + url = url, |
| 146 | + contentDisposition = contentDisposition, |
| 147 | + mimeType = mimeType, |
| 148 | + subfolder = "aFolder", |
| 149 | + userAgent = "aUserAgent" |
| 150 | + ) |
| 151 | + } |
| 152 | +} |
0 commit comments