Skip to content

Commit

Permalink
license
Browse files Browse the repository at this point in the history
  • Loading branch information
alexejn committed Oct 9, 2023
1 parent 80afe64 commit 7171edb
Show file tree
Hide file tree
Showing 46 changed files with 300 additions and 87 deletions.
11 changes: 4 additions & 7 deletions Example/Example/CategoriesListView.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
//
// FreeAPIView.swift
// Example
//
// Created by Alexey Nenastev on 4.10.23..
//
// Created by Alexey Nenastyev on 9.10.23.
// Copyright © 2023 Alexey Nenastyev (github.com/alexejn). All Rights Reserved.

import SwiftUI
import HTTPTypes
Expand Down Expand Up @@ -145,7 +142,7 @@ final class ViewModel: NSObject, ObservableObject {
func random1() async {
self.random = nil
var req = URLRequest(url: URL(string: "https://api.publicapis.org/random")!)
req.setValue("1122734-94924c70-58df-482a-811d-ff1bb0b03edf", forHTTPHeaderField: PostMock.Headers.xRequestId)
req.setValue("1122734-94924c70-58df-482a-811d-ff1bb0b03edf", forHTTPHeaderField: PostMock.Headers.xPostmanRequestId)

do {
let (data, _) = try await customURLSession.data(for: req)
Expand All @@ -159,7 +156,7 @@ final class ViewModel: NSObject, ObservableObject {
func random2() async {
self.random = nil
var req = URLRequest(url: URL(string: "https://api.publicapis.org/random")!)
req.setValue("1122734-c5751f7c-7e38-42b0-82d4-7fcfb57fc79a", forHTTPHeaderField: PostMock.Headers.xRequestId)
req.setValue("1122734-c5751f7c-7e38-42b0-82d4-7fcfb57fc79a", forHTTPHeaderField: PostMock.Headers.xPostmanRequestId)
do {
let (data, _) = try await customURLSession.data(for: req)
self.random = try JSONDecoder().decode(EntriesResponse.self, from: data).entries.first
Expand Down
8 changes: 8 additions & 0 deletions Example/Example/ContentView.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//
// Created by Alexey Nenastyev on 9.10.23.
// Copyright © 2023 Alexey Nenastyev (github.com/alexejn). All Rights Reserved.

import SwiftUI
import PostMock

Expand All @@ -9,6 +13,10 @@ struct ContentView: View {
.onAppear {
PostMock.shared.configurate(with: .myConfig)
PostMock.shared.enable()
PostMock.shared.placeholderValues =
[
"{{host}}" : { "api.publicapis.org" }
]
}
}
}
Expand Down
7 changes: 2 additions & 5 deletions Example/Example/ExampleApp.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
//
// ExampleApp.swift
// Example
//
// Created by Alexey Nenastev on 27.9.23..
//
// Created by Alexey Nenastyev on 9.10.23.
// Copyright © 2023 Alexey Nenastyev (github.com/alexejn). All Rights Reserved.

import SwiftUI

Expand Down
13 changes: 13 additions & 0 deletions PostMock.xcworkspace/xcshareddata/IDETemplateMacros.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>FILEHEADER</key>
<string>
// Created by ___ORGANIZATIONNAME___ on ___DATE___
// Copyright © ___YEAR___ ___ORGANIZATIONNAME___ (github.com/alexejn). All Rights Reserved.
</string>
<key>ORGANIZATIONNAME</key>
<string>Alexey Nenastyev</string>
</dict>
</plist>
4 changes: 4 additions & 0 deletions Sources/Networking/URLRequest+.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//
// Created by Alexey Nenastyev on 9.10.23.
// Copyright © 2023 Alexey Nenastyev (github.com/alexejn). All Rights Reserved.

import Foundation

extension URLRequest {
Expand Down
4 changes: 4 additions & 0 deletions Sources/Networking/URLResponce+.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//
// Created by Alexey Nenastyev on 9.10.23.
// Copyright © 2023 Alexey Nenastyev (github.com/alexejn). All Rights Reserved.

import Foundation

extension URLResponse {
Expand Down
4 changes: 4 additions & 0 deletions Sources/Networking/URLSession+.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//
// Created by Alexey Nenastyev on 9.10.23.
// Copyright © 2023 Alexey Nenastyev (github.com/alexejn). All Rights Reserved.

import Foundation

extension URLSession {
Expand Down
26 changes: 0 additions & 26 deletions Sources/PoctMock/PostMockPlaceholderProvider.swift

This file was deleted.

5 changes: 5 additions & 0 deletions Sources/PoctMock/PostMockURLProtocol.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//
// Created by Alexey Nenastyev on 9.10.23.
// Copyright © 2023 Alexey Nenastyev (github.com/alexejn). All Rights Reserved.

import Foundation

open class PostMockURLProtocol: URLProtocol {
Expand Down Expand Up @@ -29,6 +33,7 @@ open class PostMockURLProtocol: URLProtocol {

private class func canServeRequest(_ request: URLRequest) -> Bool {
guard PostMock.shared.isEnabled else { return false }
guard request.value(forHTTPHeaderField: PostMock.Headers.xExclude) != nil else { return false }

guard URLProtocol.property(forKey: PostMockURLProtocol.internalKey, in: request) == nil,
let url = request.url,
Expand Down
11 changes: 8 additions & 3 deletions Sources/PoctMock/PostmanAPI.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
//
// Created by Alexey Nenastyev on 9.10.23.
// Copyright © 2023 Alexey Nenastyev (github.com/alexejn). All Rights Reserved.

import Foundation

private extension URLRequest.Config {
static var postman: Self {
var config = Self.default
config.host = "api.getpostman.com"
config.headers[PostMock.Headers.xApiKey] = PostMock.shared.config.apiKey
config.headers[PostMock.Headers.xExclude] = ""
return config
}
}
Expand Down Expand Up @@ -58,15 +63,15 @@ struct PostmanAPI {

typealias CollectionItems = CollectionReponse.Collection

struct Workspace: Decodable, Equatable {
struct Workspace: Codable, Equatable {

struct Collection: Decodable, Hashable {
struct Collection: Codable, Hashable {
let id: String
let uid: String
let name: String
}

struct Mock: Decodable, Identifiable, Hashable {
struct Mock: Codable, Identifiable, Hashable {
let id: String
let name: String
}
Expand Down
8 changes: 6 additions & 2 deletions Sources/PoctMock/PostmanRequestPattern.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//
// Created by Alexey Nenastyev on 9.10.23.
// Copyright © 2023 Alexey Nenastyev (github.com/alexejn). All Rights Reserved.

import Foundation

/// Шаблон постман запроса - используется для опеределения соответсвия между описанием запроса в постмане и реальным которое делает приложение
Expand All @@ -24,7 +28,7 @@ public struct PostmanRequestPattern: Hashable, CustomStringConvertible, Codable
guard let url = request.url,
let method = request.httpMethod else { return false }

let requestUID = request.value(forHTTPHeaderField: PostMock.Headers.xRequestId)
let requestUID = request.value(forHTTPHeaderField: PostMock.Headers.xPostmanRequestId)
let originalMockedHost = request.value(forHTTPHeaderField:PostMock.Headers.xMockedHost)

return match(url: url, method: method, requestUID: requestUID, originalMockedHost: originalMockedHost)
Expand All @@ -37,7 +41,7 @@ public struct PostmanRequestPattern: Hashable, CustomStringConvertible, Codable
guard let host = url.host else { return false }
let hostPart = originalMockedHost ?? host
let urlPattern = "\(method.uppercased()) \(hostPart)\(url.path)"
let hostPlaceholderValue = PostMockPlaceholderProvider.shared[hostPlaceholder]?()
let hostPlaceholderValue = PostMock.shared.value(forPlaceholder: hostPlaceholder)
let pattern = "\(self.method.uppercased()) \(hostPlaceholderValue ?? hostPart)\(path)"
return PostmanRequestPattern.matchURL(urlPattern, postmanURLPath: pattern)
}
Expand Down
6 changes: 5 additions & 1 deletion Sources/PoctMock/PostmanRequestsMocks.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
//
// Created by Alexey Nenastyev on 9.10.23.
// Copyright © 2023 Alexey Nenastyev (github.com/alexejn). All Rights Reserved.

import Foundation

extension UserDefaults {
public static let mocks = UserDefaults(suiteName: "PostmanMocks")!
static let mocks = UserDefaults(suiteName: "PostmanMocks")!
}

public typealias MockResponseID = String
Expand Down
4 changes: 4 additions & 0 deletions Sources/PoctMock/URLRequest+PostMock.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//
// Created by Alexey Nenastyev on 9.10.23.
// Copyright © 2023 Alexey Nenastyev (github.com/alexejn). All Rights Reserved.

import Foundation

extension URLRequest {
Expand Down
4 changes: 4 additions & 0 deletions Sources/PoctMock/URLRequestCallInfos.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//
// Created by Alexey Nenastyev on 9.10.23.
// Copyright © 2023 Alexey Nenastyev (github.com/alexejn). All Rights Reserved.

import Foundation

typealias CallID = UUID
Expand Down
8 changes: 2 additions & 6 deletions Sources/PoctMock/URLSessionCallInfos+PostMock.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
//
// HTTPCallInfoDictionary+Postman.swift
// fbsData
//
// Created by Alexey Nenastev on 27.8.23..
// Copyright © 2023 Data Driven Lab. All rights reserved.
//
// Created by Alexey Nenastyev on 9.10.23.
// Copyright © 2023 Alexey Nenastyev (github.com/alexejn). All Rights Reserved.

import Foundation

Expand Down
9 changes: 7 additions & 2 deletions Sources/PostMockUI/CallsView.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//
// Created by Alexey Nenastyev on 9.10.23.
// Copyright © 2023 Alexey Nenastyev (github.com/alexejn). All Rights Reserved.

import SwiftUI

private struct CallsViewItem: View {
Expand Down Expand Up @@ -72,11 +76,12 @@ struct CallsView: View {
}
.toolbar {
ToolbarItem(placement: .rightCorner) {
Button("Clear", action: {
Button("", systemImage: "trash") {
calls = []
Task {
await dictionary.clear()
}
})
}
}
}
.onAppear {
Expand Down
3 changes: 3 additions & 0 deletions Sources/PostMockUI/Collection/CollapsableView.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//
// Created by Alexey Nenastyev on 9.10.23.
// Copyright © 2023 Alexey Nenastyev (github.com/alexejn). All Rights Reserved.

import SwiftUI

Expand Down
4 changes: 4 additions & 0 deletions Sources/PostMockUI/Collection/CollectionItems+Samples.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//
// Created by Alexey Nenastyev on 9.10.23.
// Copyright © 2023 Alexey Nenastyev (github.com/alexejn). All Rights Reserved.

import Foundation

extension CollectionItems {
Expand Down
4 changes: 4 additions & 0 deletions Sources/PostMockUI/Collection/CollectionModel.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//
// Created by Alexey Nenastyev on 9.10.23.
// Copyright © 2023 Alexey Nenastyev (github.com/alexejn). All Rights Reserved.

import Foundation
import SwiftUI

Expand Down
4 changes: 4 additions & 0 deletions Sources/PostMockUI/Collection/CollectionView.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//
// Created by Alexey Nenastyev on 9.10.23.
// Copyright © 2023 Alexey Nenastyev (github.com/alexejn). All Rights Reserved.

import SwiftUI

struct CollectionView: View {
Expand Down
4 changes: 4 additions & 0 deletions Sources/PostMockUI/Collection/FolderNode.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//
// Created by Alexey Nenastyev on 9.10.23.
// Copyright © 2023 Alexey Nenastyev (github.com/alexejn). All Rights Reserved.

import SwiftUI

struct FolderNode: View {
Expand Down
4 changes: 4 additions & 0 deletions Sources/PostMockUI/Collection/Method+Color.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
// Created by Alexey Nenastev on 4.10.23..
//

//
// Created by Alexey Nenastyev on 9.10.23.
// Copyright © 2023 Alexey Nenastyev (github.com/alexejn). All Rights Reserved.

import SwiftUI

extension String {
Expand Down
4 changes: 4 additions & 0 deletions Sources/PostMockUI/Collection/RequestLabel.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//
// Created by Alexey Nenastyev on 9.10.23.
// Copyright © 2023 Alexey Nenastyev (github.com/alexejn). All Rights Reserved.

import SwiftUI

struct RequestLabel: View {
Expand Down
6 changes: 5 additions & 1 deletion Sources/PostMockUI/Collection/RequestNode.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//
// Created by Alexey Nenastyev on 9.10.23.
// Copyright © 2023 Alexey Nenastyev (github.com/alexejn). All Rights Reserved.

import SwiftUI

struct RequestNode: View {
Expand All @@ -9,7 +13,7 @@ struct RequestNode: View {
@EnvironmentObject var mocks: PostmanRequestsMocks

var header: some View {
RequestLabel(name: "\(item.name)-\(badge)",
RequestLabel(name: item.name,
method: request.method,
url: request.url.raw,
badge: badge)
Expand Down
4 changes: 4 additions & 0 deletions Sources/PostMockUI/Collection/ResponceLabel.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//
// Created by Alexey Nenastyev on 9.10.23.
// Copyright © 2023 Alexey Nenastyev (github.com/alexejn). All Rights Reserved.

import SwiftUI

struct ResponseLabel: View {
Expand Down
26 changes: 23 additions & 3 deletions Sources/PostMockUI/Collection/ResponseDetailView.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
//
// Created by Alexey Nenastyev on 9.10.23.
// Copyright © 2023 Alexey Nenastyev (github.com/alexejn). All Rights Reserved.

import SwiftUI

struct ResponseDetailView: View {
var item: CollectionItems.Item
var response: CollectionItems.Response
Expand Down Expand Up @@ -38,12 +43,27 @@ struct ResponseDetailView: View {
}
}
.buttonStyle(.plain)
Divider()


if let pattern = item.pattern, usedForMock {
Text(pattern.description)
.foregroundColor(.blue)
Divider()
VStack(alignment: .leading) {
Text("Mocked if request match template")
.fontWeight(.light)
Text(pattern.description)
.foregroundColor(.blue)
.font(.callout)
Spacer()
.frame(height: 5)
Text("or has header \(PostMock.Headers.xPostmanRequestId) equal")
.fontWeight(.light)
Text(item.uid)
.fontWeight(.thin)
.font(.footnote)
}
.font(.footnote)
}
Divider()
}
.padding([.top, .horizontal])
.background(Color.white)
Expand Down
4 changes: 4 additions & 0 deletions Sources/PostMockUI/CurrentMocksView.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//
// Created by Alexey Nenastyev on 9.10.23.
// Copyright © 2023 Alexey Nenastyev (github.com/alexejn). All Rights Reserved.

import SwiftUI

struct CurrentMocksView: View {
Expand Down
Loading

0 comments on commit 7171edb

Please sign in to comment.