Skip to content

Commit

Permalink
Merge pull request #8 from tinkoff-mobile-tech/feature/response-header
Browse files Browse the repository at this point in the history
headers added to response
  • Loading branch information
RomanGL authored Jan 19, 2023
2 parents 138e988 + c211a9b commit d1a9d26
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 4 deletions.
22 changes: 22 additions & 0 deletions Development/Source/Core/HttpBodyStub.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// HTTPBody.swift
// TinkoffMockStrapping
//
// Created by Margarita Shishkina on 12.01.2023.
//

import Foundation
import SwiftyJSON

/// Тело запроса
/// Http body
public enum HttpBodyStub {

/// В формате json
/// JSON body
case json(JSON)

/// Тело запроса в любом формате
/// Raw data
case data(Data)
}
42 changes: 42 additions & 0 deletions Development/Source/Core/HttpResponseStub.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// HTTPResponse.swift
// TinkoffMockStrapping
//
// Created by Margarita Shishkina on 12.01.2023.
//

import Foundation
import SwiftyJSON

/// Http ответ
/// Http response
public struct HttpResponseStub: HttpResponseStubProtocol {

/// Статус код
/// Status code
public let statusCode: Int

/// Статус
/// Reason phrase
public let reasonPhrase: String

/// Заголовки
/// Headers
public let headers: [String: String]

/// Тело ответа
/// Response body
public let body: HttpBodyStub

/// Инициализатор
/// Initializer
public init(statusCode: Int = 200,
reasonPhrase: String = "OK",
headers: [String: String] = [:],
body: HttpBodyStub = .json(JSON())) {
self.statusCode = statusCode
self.reasonPhrase = reasonPhrase
self.headers = headers
self.body = body
}
}
4 changes: 4 additions & 0 deletions Development/Source/Core/NetworkStubResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ public enum NetworkStubResponse {
/// Network error
/// - Warning: this error doesn't work in UI tests
case connectionError

/// Ответ http сервера, с возможностью указать заголовки и код ответа
/// Http response with headers and status code
case response(HttpResponseStubProtocol)
}
29 changes: 29 additions & 0 deletions Development/Source/Core/Protocols/HttpResponseStubProtocol.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// IHTTPResponse.swift
// TinkoffMockStrapping
//
// Created by Margarita Shishkina on 12.01.2023.
//

import Foundation

/// Протокол ответа сервера
/// Http response protocol
public protocol HttpResponseStubProtocol {

/// Статус код
/// Status code
var statusCode: Int { get }

/// Статус
/// Reason phrase
var reasonPhrase: String { get }

/// Заголовки
/// Headers
var headers: [String: String] { get }

/// Тело ответа
/// Response body
var body: HttpBodyStub { get }
}
14 changes: 14 additions & 0 deletions Development/Source/Swifter/MockNetworkServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,20 @@ private extension MockNetworkServer {
fatalError("ATTENTION! " +
"The `\(String(describing: responseStub.response))` " +
"does not supported in the UI tests.")
case let .response(response):
guard let data = processHTTPBody(response.body) else {
return .internalServerError
}
return .raw(response.statusCode, response.reasonPhrase, response.headers, { try? $0.write(data) })
}
}

func processHTTPBody(_ body: HttpBodyStub) -> Data? {
switch body {
case let .json(json):
return json.rawData()
case let .data(data):
return data
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ PODS:
- SwiftGen (6.1.0)
- SwiftLint (0.35.0)
- SwiftyJSON (5.0.1)
- TinkoffMockStrapping/Core (0.2.0):
- TinkoffMockStrapping/Core (0.3.0):
- SwiftyJSON (~> 5.0.0)
- TinkoffMockStrapping/Swifter (0.2.0):
- TinkoffMockStrapping/Swifter (0.3.0):
- Swifter (~> 1.5.0)
- TinkoffMockStrapping/Core

Expand Down Expand Up @@ -39,7 +39,7 @@ SPEC CHECKSUMS:
SwiftGen: f872ca75cbd17bf7103c17f13dcfa0d9a15667b0
SwiftLint: 5553187048b900c91aa03552807681bb6b027846
SwiftyJSON: 2f33a42c6fbc52764d96f13368585094bfd8aa5e
TinkoffMockStrapping: e40a0c882c45c964281e012e79e5171f22699b28
TinkoffMockStrapping: b2fe4b4dbdede0fda2fdc83bb7d3fa80af821c38

PODFILE CHECKSUM: 9c6580bce35963e57370484f53cbdfcd5674cf4c

Expand Down
2 changes: 1 addition & 1 deletion TinkoffMockStrapping.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Pod::Spec.new do |s|

s.name = 'TinkoffMockStrapping'
s.summary = 'Library for unifying the approach to network mocking in unit- & UI-tests.'
s.version = '0.2.0'
s.version = '0.3.0'
s.homepage = 'https://github.com/tinkoff-mobile-tech/TinkoffMockStrapping'

# ――― Spec License ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
Expand Down

0 comments on commit d1a9d26

Please sign in to comment.