Skip to content

Commit

Permalink
Updates tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ljubinkovicdj93 committed Feb 22, 2023
1 parent 7647a3b commit 82be4ef
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 24 deletions.
15 changes: 7 additions & 8 deletions Tests/MoyaTests/EndpointClosureSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ final class EndpointClosureSpec: QuickSpec {

switch target.task {
case let .uploadMultipart(multipartFormData):
let additional = Moya.MultipartFormData(provider: .data("test2".data(using: .utf8)!), name: "test2")
var newMultipartFormData = multipartFormData
newMultipartFormData.append(additional)
let additionalBodyPart = Moya.MultipartFormBodyPart(provider: .data("test2".data(using: .utf8)!), name: "test2")
let newMultipartFormData = MultipartFormData(parts: multipartFormData.parts + CollectionOfOne(additionalBodyPart))
task = .uploadMultipart(newMultipartFormData)
default:
task = target.task
Expand All @@ -31,16 +30,16 @@ final class EndpointClosureSpec: QuickSpec {
}

it("appends additional multipart body in endpointClosure") {
let multipartData1 = Moya.MultipartFormData(provider: .data("test1".data(using: .utf8)!), name: "test1")
let multipartData2 = Moya.MultipartFormData(provider: .data("test2".data(using: .utf8)!), name: "test2")
let multipartData1 = Moya.MultipartFormBodyPart(provider: .data("test1".data(using: .utf8)!), name: "test1")
let multipartData2 = Moya.MultipartFormBodyPart(provider: .data("test2".data(using: .utf8)!), name: "test2")

let providedMultipartData = [multipartData1]
let sentMultipartData = [multipartData1, multipartData2]
let providedMultipartData: Moya.MultipartFormData = [multipartData1]
let sentMultipartData: Moya.MultipartFormData = [multipartData1, multipartData2]

_ = provider.request(.uploadMultipart(providedMultipartData, nil)) { _ in }
let stringData1 = session.uploadMultipartString!

let requestMultipartFormData = RequestMultipartFormData()
let requestMultipartFormData = RequestMultipartFormData(fileManager: sentMultipartData.fileManager, boundary: sentMultipartData.boundary)
requestMultipartFormData.applyMoyaMultipartFormData(sentMultipartData)
let stringData2 = String(decoding: try! requestMultipartFormData.encode(), as: UTF8.self)

Expand Down
20 changes: 10 additions & 10 deletions Tests/MoyaTests/EndpointSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -369,38 +369,38 @@ final class EndpointSpec: QuickSpec {
describe("given endpoint comparison") {
context("when task is .uploadMultipart") {
it("should correctly acknowledge as equal for the same url, headers and form data") {
endpoint = endpoint.replacing(task: .uploadMultipart([MultipartFormData(provider: .data("test".data(using: .utf8)!), name: "test")]))
let endpointToCompare = endpoint.replacing(task: .uploadMultipart([MultipartFormData(provider: .data("test".data(using: .utf8)!), name: "test")]))
endpoint = endpoint.replacing(task: .uploadMultipart([MultipartFormBodyPart(provider: .data("test".data(using: .utf8)!), name: "test")]))
let endpointToCompare = endpoint.replacing(task: .uploadMultipart([MultipartFormBodyPart(provider: .data("test".data(using: .utf8)!), name: "test")]))

expect(endpoint) == endpointToCompare
}

it("should correctly acknowledge as not equal for the same url, headers and different form data") {
endpoint = endpoint.replacing(task: .uploadMultipart([MultipartFormData(provider: .data("test".data(using: .utf8)!), name: "test")]))
let endpointToCompare = endpoint.replacing(task: .uploadMultipart([MultipartFormData(provider: .data("test1".data(using: .utf8)!), name: "test")]))
endpoint = endpoint.replacing(task: .uploadMultipart([MultipartFormBodyPart(provider: .data("test".data(using: .utf8)!), name: "test")]))
let endpointToCompare = endpoint.replacing(task: .uploadMultipart([MultipartFormBodyPart(provider: .data("test1".data(using: .utf8)!), name: "test")]))

expect(endpoint) != endpointToCompare
}
}

context("when task is .uploadCompositeMultipart") {
it("should correctly acknowledge as equal for the same url, headers and form data") {
endpoint = endpoint.replacing(task: .uploadCompositeMultipart([MultipartFormData(provider: .data("test".data(using: .utf8)!), name: "test")], urlParameters: [:]))
let endpointToCompare = endpoint.replacing(task: .uploadCompositeMultipart([MultipartFormData(provider: .data("test".data(using: .utf8)!), name: "test")], urlParameters: [:]))
endpoint = endpoint.replacing(task: .uploadCompositeMultipart([MultipartFormBodyPart(provider: .data("test".data(using: .utf8)!), name: "test")], urlParameters: [:]))
let endpointToCompare = endpoint.replacing(task: .uploadCompositeMultipart([MultipartFormBodyPart(provider: .data("test".data(using: .utf8)!), name: "test")], urlParameters: [:]))

expect(endpoint) == endpointToCompare
}

it("should correctly acknowledge as not equal for the same url, headers and different form data") {
endpoint = endpoint.replacing(task: .uploadCompositeMultipart([MultipartFormData(provider: .data("test".data(using: .utf8)!), name: "test")], urlParameters: [:]))
let endpointToCompare = endpoint.replacing(task: .uploadCompositeMultipart([MultipartFormData(provider: .data("test1".data(using: .utf8)!), name: "test")], urlParameters: [:]))
endpoint = endpoint.replacing(task: .uploadCompositeMultipart([MultipartFormBodyPart(provider: .data("test".data(using: .utf8)!), name: "test")], urlParameters: [:]))
let endpointToCompare = endpoint.replacing(task: .uploadCompositeMultipart([MultipartFormBodyPart(provider: .data("test1".data(using: .utf8)!), name: "test")], urlParameters: [:]))

expect(endpoint) != endpointToCompare
}

it("should correctly acknowledge as not equal for the same url, headers and different url parameters") {
endpoint = endpoint.replacing(task: .uploadCompositeMultipart([MultipartFormData(provider: .data("test".data(using: .utf8)!), name: "test")], urlParameters: ["test": "test2"]))
let endpointToCompare = endpoint.replacing(task: .uploadCompositeMultipart([MultipartFormData(provider: .data("test".data(using: .utf8)!), name: "test")], urlParameters: ["test": "test3"]))
endpoint = endpoint.replacing(task: .uploadCompositeMultipart([MultipartFormBodyPart(provider: .data("test".data(using: .utf8)!), name: "test")], urlParameters: ["test": "test2"]))
let endpointToCompare = endpoint.replacing(task: .uploadCompositeMultipart([MultipartFormBodyPart(provider: .data("test".data(using: .utf8)!), name: "test")], urlParameters: ["test": "test3"]))

expect(endpoint) != endpointToCompare
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/MoyaTests/MultipartFormDataSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ final class MultiPartFormData: QuickSpec {
override func spec() {
it("initializes correctly") {
let fileURL = URL(fileURLWithPath: "/tmp.txt")
let data = MultipartFormData(
let data = MultipartFormBodyPart(
provider: .file(fileURL),
name: "MyName",
fileName: "tmp.txt",
Expand Down
10 changes: 5 additions & 5 deletions Tests/MoyaTests/TestHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ enum HTTPBin: TargetType, AccessTokenAuthorizable {
case bearer
case post
case upload(file: URL)
case uploadMultipart([MultipartFormData], [String: Any]?)
case validatedUploadMultipart([MultipartFormData], [String: Any]?, [Int])
case uploadMultipart(MultipartFormData, [String: Any]?)
case validatedUploadMultipart(MultipartFormData, [String: Any]?, [Int])

var baseURL: URL { URL(string: "http://httpbin.org")! }
var path: String {
Expand Down Expand Up @@ -184,15 +184,15 @@ extension GitHubUserContent: TargetType {
// MARK: - Upload Multipart Helpers

extension HTTPBin {
static func createTestMultipartFormData() -> [MultipartFormData] {
static func createTestMultipartFormData() -> MultipartFormData {
let url = testImageUrl
let string = "some data"
guard let data = string.data(using: .utf8) else {
fatalError("Failed creating Data from String \(string)")
}
return [
MultipartFormData(provider: .file(url), name: "file", fileName: "testImage"),
MultipartFormData(provider: .data(data), name: "data")
MultipartFormBodyPart(provider: .file(url), name: "file", fileName: "testImage"),
MultipartFormBodyPart(provider: .data(data), name: "data")
]
}
}
Expand Down

0 comments on commit 82be4ef

Please sign in to comment.