Skip to content

Commit

Permalink
Request now supports adding additional content types for validation.
Browse files Browse the repository at this point in the history
This change addresses the challenges faced in both #28 and #29.
  • Loading branch information
cnoon committed Oct 10, 2015
1 parent 62b58a4 commit 3e5b99c
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 3e5b99c

Please sign in to comment.