Skip to content

Commit

Permalink
Merge pull request #60 from ishkawa/feature/fix-url-encoding
Browse files Browse the repository at this point in the history
Fix parameter encoding in GET request
  • Loading branch information
ishkawa committed Aug 24, 2015
2 parents 65ab24c + d989ce8 commit 03ebd7f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
6 changes: 6 additions & 0 deletions APIKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
19C16B541B83327A001D77CC /* RequestTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19C16B531B83327A001D77CC /* RequestTests.swift */; };
19C16B551B83327A001D77CC /* RequestTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19C16B531B83327A001D77CC /* RequestTests.swift */; };
7F08699A1A978790001AD3E1 /* APIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7F45FCFE1A94D04D006863BB /* APIKit.framework */; };
7F0869A01A9787AF001AD3E1 /* RequestBodyBuilderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F30A8551A975BD600A8C136 /* RequestBodyBuilderTests.swift */; };
7F0869A11A9787AF001AD3E1 /* ResponseBodyParserTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7FEC5A181A96FE2600B1D3C0 /* ResponseBodyParserTests.swift */; };
Expand Down Expand Up @@ -83,6 +85,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
19C16B531B83327A001D77CC /* RequestTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RequestTests.swift; sourceTree = "<group>"; };
7F0869941A978790001AD3E1 /* APIKitTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = APIKitTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
7F0869A51A978BCA001AD3E1 /* URLEncodedSerialization.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = URLEncodedSerialization.swift; sourceTree = "<group>"; };
7F1B190A1AA2CA1300C7AFCF /* APITests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = APITests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -195,6 +198,7 @@
isa = PBXGroup;
children = (
7F1B190A1AA2CA1300C7AFCF /* APITests.swift */,
19C16B531B83327A001D77CC /* RequestTests.swift */,
7F30A8551A975BD600A8C136 /* RequestBodyBuilderTests.swift */,
7FEC5A181A96FE2600B1D3C0 /* ResponseBodyParserTests.swift */,
7FEC5A161A96FE2600B1D3C0 /* Supporting Files */,
Expand Down Expand Up @@ -387,6 +391,7 @@
buildActionMask = 2147483647;
files = (
7F1B190C1AA2CA1300C7AFCF /* APITests.swift in Sources */,
19C16B551B83327A001D77CC /* RequestTests.swift in Sources */,
7F0869A01A9787AF001AD3E1 /* RequestBodyBuilderTests.swift in Sources */,
7F0869A11A9787AF001AD3E1 /* ResponseBodyParserTests.swift in Sources */,
);
Expand Down Expand Up @@ -425,6 +430,7 @@
buildActionMask = 2147483647;
files = (
7F1B190B1AA2CA1300C7AFCF /* APITests.swift in Sources */,
19C16B541B83327A001D77CC /* RequestTests.swift in Sources */,
7FEC5A191A96FE2600B1D3C0 /* ResponseBodyParserTests.swift in Sources */,
7F30A8561A975BD600A8C136 /* RequestBodyBuilderTests.swift in Sources */,
);
Expand Down
2 changes: 1 addition & 1 deletion APIKit/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public extension Request {

switch method {
case .GET, .HEAD, .DELETE:
components.query = URLEncodedSerialization.stringFromDictionary(parameters)
components.percentEncodedQuery = URLEncodedSerialization.stringFromDictionary(parameters)

default:
do {
Expand Down
53 changes: 53 additions & 0 deletions APIKitTests/RequestTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import XCTest
import OHHTTPStubs
import APIKit

class RequestTests: XCTestCase {
struct SearchRequest: MockAPIRequest {
let query: String

// MARK: Request
typealias Response = [String: AnyObject]

var method: HTTPMethod {
return .GET
}

var path: String {
return "/"
}

var parameters: [String: AnyObject] {
return [
"q": query,
]
}

func responseFromObject(object: AnyObject, URLResponse: NSHTTPURLResponse) -> Response? {
return object as? [String: AnyObject]
}
}

override func tearDown() {
OHHTTPStubs.removeAllStubs()
super.tearDown()
}

func testURLQueryParameterEncoding() {
OHHTTPStubs.stubRequestsPassingTest({ request in
XCTAssert(request.URL?.query == "q=%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF")
return true
}, withStubResponse: { request in
return OHHTTPStubsResponse(data: NSData(), statusCode: 200, headers: nil)
})

let request = SearchRequest(query: "こんにちは")
let expectation = expectationWithDescription("waiting for the response.")

API.sendRequest(request) { result in
expectation.fulfill()
}

waitForExpectationsWithTimeout(1.0, handler: nil)
}
}

0 comments on commit 03ebd7f

Please sign in to comment.