Skip to content

Commit b66dd93

Browse files
committed
test: custom headers
1 parent 2b14bd8 commit b66dd93

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

OSInAppBrowserLib/WebView/OSIABWebViewModel.swift

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ class OSIABWebViewModel: NSObject, ObservableObject {
4242
/// Constructor method.
4343
/// - Parameters:
4444
/// - url: The current URL being displayed
45-
/// - webViewConfiguration: Collection of properties with which to initialize the WebView.
45+
/// - webView: The WebView to display and configure.
4646
/// - scrollViewBounces: Indicates if the WebView's bounce property should be enabled. Defaults to `true`.
4747
/// - customUserAgent: Sets a custom user agent for the WebView.
4848
/// - uiModel: Collection of properties to apply to the WebView's interface.
4949
/// - callbackHandler: Object that manages all the callbacks available for the WebView.
5050
init(
5151
url: URL,
5252
customHeaders: [String: String]? = nil,
53-
_ webViewConfiguration: WKWebViewConfiguration,
53+
_ webView: WKWebView,
5454
_ scrollViewBounces: Bool = true,
5555
_ customUserAgent: String? = nil,
5656
_ backForwardNavigationGestures: Bool = true,
@@ -59,7 +59,7 @@ class OSIABWebViewModel: NSObject, ObservableObject {
5959
) {
6060
self.url = url
6161
self.customHeaders = customHeaders
62-
self.webView = .init(frame: .zero, configuration: webViewConfiguration)
62+
self.webView = webView
6363
self.closeButtonText = uiModel.closeButtonText
6464
self.callbackHandler = callbackHandler
6565
if uiModel.showToolbar {
@@ -82,6 +82,36 @@ class OSIABWebViewModel: NSObject, ObservableObject {
8282
self.setupBindings(uiModel.showURL, uiModel.showToolbar, uiModel.showNavigationButtons)
8383
}
8484

85+
/// Constructor method.
86+
/// - Parameters:
87+
/// - url: The current URL being displayed
88+
/// - webViewConfiguration: Collection of properties with which to initialize the WebView.
89+
/// - scrollViewBounces: Indicates if the WebView's bounce property should be enabled. Defaults to `true`.
90+
/// - customUserAgent: Sets a custom user agent for the WebView.
91+
/// - uiModel: Collection of properties to apply to the WebView's interface.
92+
/// - callbackHandler: Object that manages all the callbacks available for the WebView.
93+
convenience init(
94+
url: URL,
95+
customHeaders: [String: String]? = nil,
96+
_ webViewConfiguration: WKWebViewConfiguration,
97+
_ scrollViewBounces: Bool = true,
98+
_ customUserAgent: String? = nil,
99+
_ backForwardNavigationGestures: Bool = true,
100+
uiModel: OSIABWebViewUIModel,
101+
callbackHandler: OSIABWebViewCallbackHandler
102+
) {
103+
self.init(
104+
url: url,
105+
customHeaders: customHeaders,
106+
WKWebView(frame: .zero, configuration: webViewConfiguration),
107+
scrollViewBounces,
108+
customUserAgent,
109+
backForwardNavigationGestures,
110+
uiModel: uiModel,
111+
callbackHandler: callbackHandler
112+
)
113+
}
114+
85115
/// Setups the combine bindings, so that the Published properties can be filled automatically and reactively.
86116
private func setupBindings(_ showURL: Bool, _ showToolbar: Bool, _ showNavigationButtons: Bool) {
87117
if #available(iOS 14.0, *) {

OSInAppBrowserLibTests/OSIABViewModelTests.swift

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,15 @@ final class OSIABViewModelTests: XCTestCase {
136136
XCTAssertEqual(sut.webView.url, url)
137137
}
138138

139+
func test_loadURL_withCustomHeaders() {
140+
let mockWebView = MockWKWebView(frame: .zero, configuration: WKWebViewConfiguration())
141+
let sut = makeSUT(url, webView: mockWebView, customHeaders: ["Custom-Header": "Value"])
142+
143+
sut.loadURL()
144+
145+
XCTAssertEqual(mockWebView.lastRequest?.value(forHTTPHeaderField: "Custom-Header"), "Value")
146+
}
147+
139148
// MARK: Close Button Pressed
140149

141150
func test_closeButtonPressed_triggersTheBrowserClosedEvent() {
@@ -275,6 +284,8 @@ final class OSIABViewModelTests: XCTestCase {
275284
private extension OSIABViewModelTests {
276285
func makeSUT(
277286
_ url: URL,
287+
webView: WKWebView? = nil,
288+
customHeaders: [String: String]? = nil,
278289
mediaTypesRequiringUserActionForPlayback: WKAudiovisualMediaTypes = [],
279290
ignoresViewportScaleLimits: Bool = false,
280291
allowsInlineMediaPlayback: Bool = false,
@@ -298,7 +309,8 @@ private extension OSIABViewModelTests {
298309

299310
return .init(
300311
url: url,
301-
configurationModel.toWebViewConfiguration(),
312+
customHeaders: customHeaders,
313+
webView ?? WKWebView(frame: .zero, configuration: configurationModel.toWebViewConfiguration()),
302314
scrollViewBounds,
303315
customUserAgent,
304316
uiModel: .init(
@@ -318,3 +330,11 @@ private extension OSIABViewModelTests {
318330
)
319331
}
320332
}
333+
334+
class MockWKWebView: WKWebView {
335+
var lastRequest: URLRequest?
336+
override func load(_ request: URLRequest) -> WKNavigation? {
337+
lastRequest = request
338+
return nil
339+
}
340+
}

0 commit comments

Comments
 (0)