Skip to content

Commit

Permalink
Merge pull request #33 from Alamofire/feature/request_image_content_t…
Browse files Browse the repository at this point in the history
…ypes

Feature - Request Image Content Types
  • Loading branch information
cnoon committed Oct 14, 2015
2 parents aed5d86 + 3e5b99c commit 1ce7c6b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 15 deletions.
36 changes: 22 additions & 14 deletions Source/Request+AlamofireImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,27 @@ import Cocoa
#endif

extension Request {
static var acceptableImageContentTypes: Set<String> = [
"image/tiff",
"image/jpeg",
"image/gif",
"image/png",
"image/ico",
"image/x-icon",
"image/bmp",
"image/x-bmp",
"image/x-xbitmap",
"image/x-win-bitmap"
]

/**
Adds the content types specified to the list of acceptable images content types for validation.

- parameter contentTypes: The additional content types.
*/
public class func addAcceptableImageContentTypes(contentTypes: Set<String>) {
Request.acceptableImageContentTypes.unionInPlace(contentTypes)
}

// MARK: - iOS and watchOS

Expand Down Expand Up @@ -195,20 +216,7 @@ extension Request {
return true
}

let acceptableContentTypes: Set<String> = [
"image/tiff",
"image/jpeg",
"image/gif",
"image/png",
"image/ico",
"image/x-icon",
"image/bmp",
"image/x-bmp",
"image/x-xbitmap",
"image/x-win-bitmap"
]

if let mimeType = response?.MIMEType where acceptableContentTypes.contains(mimeType) {
if let mimeType = response?.MIMEType where Request.acceptableImageContentTypes.contains(mimeType) {
return true
}

Expand Down
31 changes: 30 additions & 1 deletion Tests/RequestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,40 @@
// THE SOFTWARE.

import Alamofire
import AlamofireImage
@testable import AlamofireImage
import Foundation
import XCTest

class RequestTestCase: BaseTestCase {
var acceptableImageContentTypes: Set<String>!

// MARK: - Setup and Teardown

override func setUp() {
super.setUp()
acceptableImageContentTypes = Request.acceptableImageContentTypes
}

override func tearDown() {
super.tearDown()
Request.acceptableImageContentTypes = acceptableImageContentTypes
}

// MARK: - Image Content Type Tests

func testThatAddingAcceptableImageContentTypesInsertsThemIntoTheGlobalList() {
// Given
let contentTypes: Set<String> = ["image/jpg", "binary/octet-stream"]

// When
let beforeCount = Request.acceptableImageContentTypes.count
Request.addAcceptableImageContentTypes(contentTypes)
let afterCount = Request.acceptableImageContentTypes.count

// Then
XCTAssertEqual(beforeCount, 10, "before count should be 10")
XCTAssertEqual(afterCount, 12, "after count should be 12")
}

// MARK: - Image Serialization Tests

Expand Down

0 comments on commit 1ce7c6b

Please # to comment.