-
Notifications
You must be signed in to change notification settings - Fork 3
feat: support user-supplied literal headers #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+434
−64
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
15 changes: 12 additions & 3 deletions
15
...Desktop/Coder Desktop.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
Coder Desktop/Coder Desktop/Views/Settings/GeneralTab.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import LaunchAtLogin | ||
import SwiftUI | ||
|
||
struct GeneralTab: View { | ||
var body: some View { | ||
Form { | ||
Section { | ||
LaunchAtLogin.Toggle("Launch at Login") | ||
} | ||
}.formStyle(.grouped) | ||
} | ||
} | ||
|
||
#Preview { | ||
GeneralTab() | ||
} |
45 changes: 45 additions & 0 deletions
45
Coder Desktop/Coder Desktop/Views/Settings/LiteralHeaderModal.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import SwiftUI | ||
|
||
struct LiteralHeaderModal: View { | ||
var existingHeader: LiteralHeader? | ||
|
||
@EnvironmentObject var settings: Settings | ||
@Environment(\.dismiss) private var dismiss | ||
|
||
@State private var header: String = "" | ||
@State private var value: String = "" | ||
|
||
var body: some View { | ||
VStack(spacing: 0) { | ||
Form { | ||
Section { | ||
TextField("Header", text: $header) | ||
TextField("Value", text: $value) | ||
} | ||
}.formStyle(.grouped).scrollDisabled(true).padding(.horizontal) | ||
Divider() | ||
HStack { | ||
Spacer() | ||
Button("Cancel", action: { dismiss() }).keyboardShortcut(.cancelAction) | ||
Button(existingHeader == nil ? "Add" : "Save", action: submit) | ||
.keyboardShortcut(.defaultAction) | ||
}.padding(20) | ||
}.onAppear { | ||
if let existingHeader { | ||
self.header = existingHeader.header | ||
self.value = existingHeader.value | ||
} | ||
} | ||
} | ||
|
||
func submit() { | ||
defer { dismiss() } | ||
if let existingHeader { | ||
settings.literalHeaders.removeAll { $0 == existingHeader } | ||
} | ||
let newHeader = LiteralHeader(header: header, value: value) | ||
if !settings.literalHeaders.contains(newHeader) { | ||
settings.literalHeaders.append(newHeader) | ||
} | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
Coder Desktop/Coder Desktop/Views/Settings/LiteralHeadersSection.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import SwiftUI | ||
|
||
struct LiteralHeadersSection<VPN: VPNService>: View { | ||
@EnvironmentObject var vpn: VPN | ||
@EnvironmentObject var settings: Settings | ||
|
||
@State private var selectedHeader: LiteralHeader.ID? | ||
@State private var editingHeader: LiteralHeader? | ||
@State private var addingNewHeader = false | ||
|
||
let inspection = Inspection<Self>() | ||
|
||
var body: some View { | ||
Section { | ||
Toggle(isOn: settings.$useLiteralHeaders) { | ||
Text("HTTP Headers") | ||
Text("When enabled, these headers will be included on all outgoing HTTP requests.") | ||
if vpn.state != .disabled { Text("Cannot be modified while Coder VPN is enabled.") } | ||
} | ||
.controlSize(.large) | ||
|
||
Table(settings.literalHeaders, selection: $selectedHeader) { | ||
TableColumn("Header", value: \.header) | ||
TableColumn("Value", value: \.value) | ||
}.opacity(settings.useLiteralHeaders ? 1 : 0.5) | ||
.frame(minWidth: 400, minHeight: 200) | ||
.padding(.bottom, 25) | ||
.overlay(alignment: .bottom) { | ||
VStack(alignment: .leading, spacing: 0) { | ||
Divider() | ||
HStack(spacing: 0) { | ||
Button { | ||
addingNewHeader = true | ||
} label: { | ||
Image(systemName: "plus") | ||
.frame(width: 24, height: 24) | ||
} | ||
Divider() | ||
Button { | ||
settings.literalHeaders.removeAll { $0.id == selectedHeader } | ||
selectedHeader = nil | ||
} label: { | ||
Image(systemName: "minus") | ||
.frame(width: 24, height: 24) | ||
}.disabled(selectedHeader == nil) | ||
} | ||
.buttonStyle(.borderless) | ||
} | ||
.background(.primary.opacity(0.04)) | ||
.fixedSize(horizontal: false, vertical: true) | ||
} | ||
.background(.primary.opacity(0.04)) | ||
.contextMenu(forSelectionType: LiteralHeader.ID.self, menu: { _ in }, | ||
primaryAction: { selectedHeaders in | ||
if let firstHeader = selectedHeaders.first { | ||
editingHeader = settings.literalHeaders.first(where: { $0.id == firstHeader }) | ||
} | ||
}) | ||
.disabled(!settings.useLiteralHeaders) | ||
} | ||
.sheet(isPresented: $addingNewHeader) { | ||
LiteralHeaderModal() | ||
} | ||
.sheet(item: $editingHeader) { header in | ||
LiteralHeaderModal(existingHeader: header) | ||
}.onTapGesture { | ||
selectedHeader = nil | ||
}.disabled(vpn.state != .disabled) | ||
.onReceive(inspection.notice) { self.inspection.visit(self, $0) } // ViewInspector | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
Coder Desktop/Coder Desktop/Views/Settings/NetworkTab.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import SwiftUI | ||
|
||
struct NetworkTab<VPN: VPNService>: View { | ||
var body: some View { | ||
Form { | ||
LiteralHeadersSection<VPN>() | ||
} | ||
.formStyle(.grouped) | ||
} | ||
} | ||
|
||
#Preview { | ||
NetworkTab<PreviewVPN>() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import SwiftUI | ||
|
||
struct SettingsView<VPN: VPNService>: View { | ||
@AppStorage("SettingsSelectedIndex") private var selection: SettingsTab = .general | ||
|
||
var body: some View { | ||
TabView(selection: $selection) { | ||
GeneralTab() | ||
.tabItem { | ||
Label("General", systemImage: "gearshape") | ||
}.tag(SettingsTab.general) | ||
NetworkTab<VPN>() | ||
.tabItem { | ||
Label("Network", systemImage: "dot.radiowaves.left.and.right") | ||
}.tag(SettingsTab.network) | ||
}.frame(width: 600) | ||
.frame(maxHeight: 500) | ||
.scrollContentBackground(.hidden) | ||
.fixedSize() | ||
} | ||
} | ||
|
||
enum SettingsTab: Int { | ||
case general | ||
case network | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import Combine | ||
import SwiftUI | ||
|
||
// This is required for inspecting stateful views | ||
final class Inspection<V> { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
Coder Desktop/Coder DesktopTests/LiteralHeadersSettingTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
@testable import Coder_Desktop | ||
import SwiftUI | ||
import Testing | ||
import ViewInspector | ||
|
||
@MainActor | ||
@Suite(.timeLimit(.minutes(1))) | ||
struct LiteralHeadersSettingTests { | ||
let vpn: MockVPNService | ||
let sut: LiteralHeadersSection<MockVPNService> | ||
let view: any View | ||
|
||
init() { | ||
vpn = MockVPNService() | ||
sut = LiteralHeadersSection<MockVPNService>() | ||
let store = UserDefaults(suiteName: #file)! | ||
store.removePersistentDomain(forName: #file) | ||
view = sut.environmentObject(vpn).environmentObject(Settings(store: store)) | ||
} | ||
|
||
@Test | ||
func testToggleDisabledWhenVPNEnabled() async throws { | ||
vpn.state = .connected | ||
|
||
try await ViewHosting.host(view) { | ||
try await sut.inspection.inspect { view in | ||
let toggle = try view.find(ViewType.Toggle.self) | ||
#expect(toggle.isDisabled()) | ||
#expect(throws: Never.self) { try toggle.labelView().find(text: "HTTP Headers") } | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
func testToggleEnabledWhenVPNDisabled() async throws { | ||
vpn.state = .disabled | ||
|
||
try await ViewHosting.host(view) { | ||
try await sut.inspection.inspect { view in | ||
let toggle = try view.find(ViewType.Toggle.self) | ||
#expect(!toggle.isDisabled()) | ||
#expect(throws: Never.self) { try toggle.labelView().find(text: "HTTP Headers") } | ||
} | ||
} | ||
} | ||
|
||
// TODO: More tests, ViewInspector cannot currently inspect Tables | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was debugging a CI-only test failure and
xcpretty
was swallowing a helpful error log -xcbeautify
seems like it provides better output in general for tests.For context, the default xcodebuild output is thousands of lines, and not at all readable.