Skip to content
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

Feature Request: Add Ability to Pass Custom Header for Verification #95

Open
kelliepersson opened this issue Sep 29, 2022 · 14 comments
Open
Labels
enhancement New feature or request

Comments

@kelliepersson
Copy link

🗣 Context

Would like to ProviderVerifier.Options to support --header <custom-header> option

💬 Narrative

When I run my PactVerifiable conforming tests
I want to be able to pass in a header option
So that I can inject the header with an authorization token

📝 Notes

According to the pact_verifier_cli documentation , passing a custom header is one of the available options.

--header <custom-header>...
            Add a custom header to be included in the calls to the provider. Values must be in the
            form KEY=VALUE, where KEY and VALUE contain ASCII characters (32-127) only. Can be
            repeated.

🏗 Design

✅ Acceptance Criteria

GIVEN header = ["Authorization" : "sometokenhere"

WHEN
ProviderVerifier.Options is initialized with custom header value

options = ProviderVerifier.Options(
            provider: provider,
            pactsSource: .broker(pactBroker),
            customHeader: header,
            logLevel: .error
        )

THEN
Provider Verification is successful and custom header to pact_verifier_cli is passed in the option list.

🚫 Out of Scope

@kelliepersson kelliepersson added the enhancement New feature or request label Sep 29, 2022
@kelliepersson kelliepersson changed the title Add Ability to Pass Custom Header for Verification feature: Add Ability to Pass Custom Header for Verification Sep 29, 2022
@kelliepersson kelliepersson changed the title feature: Add Ability to Pass Custom Header for Verification Feature Request: Add Ability to Pass Custom Header for Verification Sep 29, 2022
@surpher
Copy link
Owner

surpher commented Oct 1, 2022

@kelliepersson, have a go at this by setting the branch as your source for PactSwift dependency:

.package(
    url: "https://github.com/surpher/PactSwift.git",
    branch: "feature/provider_verification_headers_option"
)

If it works as expected I'll merge the PR (#96) and create a new release.

@kelliepersson
Copy link
Author

@surpher Thanks so much for jumping on this so quickly!!! I am testing now. Will let you know asap.

@kelliepersson
Copy link
Author

kelliepersson commented Oct 3, 2022

@surpher I am not able to get the branch to resolve the dependency,

pactswift is required using two different revision-based requirements (feature/provider_verification_headers_option and main), which is not supported

Xcode [Version 13.4.1 (13F100)] will not drop PactSwift on main branch.

@surpher
Copy link
Owner

surpher commented Oct 4, 2022

I'm not exactly clear how and what exactly you've updated in your project setup to be presented with that error. Sounds like a setup issue.

Editing the repository for PactSwift package and setting a branch in Xcode should work:
Screen Shot 2022-10-04 at 1 43 46 pm

Perhaps remove the package first before adding it with the reference to the branch?

@kelliepersson
Copy link
Author

@surpher it's definitely an error on my end 😅 . I will spend more time today trying to resolve it. The problem is my Xcode will not remove the package 😩. Searched around and this seems to be a problem others have had as well. Hoping to resolve this by eod. Thanks again for all your help. Will let you know.

@kelliepersson
Copy link
Author

@surpher Package resolution is still failing for me. Seems PackMockServer has a dependency on main branch as well.

Screen Shot 2022-10-05 at 10 21 43 AM

@surpher
Copy link
Owner

surpher commented Oct 5, 2022

What sort of a project are you working on? Does it by any chance have Package.swift files but Xcode is trying to set a new separate one and there is a conflict?
All transient Pact dependencies remained set to version number.

@surpher
Copy link
Owner

surpher commented Oct 5, 2022

I can merge it into main and not make a release. It's not a big or breaking change.

@kelliepersson
Copy link
Author

kelliepersson commented Oct 5, 2022

Thank you @surpher. I was originally trying to test this against a work project. We have Package.swift files. So instead, I just created a simple project and pointed to your branch. I was able to pass in the new customHeaders dictionary.

Finally able to get my test to run 😄, but ended up failing w/

failed - Provider Verification Error: Invalid arguments were provided to the verification process.

@surpher
Copy link
Owner

surpher commented Oct 7, 2022

Dug deeper into this and it looks like the interface PactSwiftMockServer uses for this is deprecated and does not support this argument.
I'll have to refactor how PactSwiftMockServer interfaces with pactffi written in rust first. It should be using handle based methods. It might take some more time to do though.

Update:
I've had a chat with pactffi maintainers that handle the core implementation written in rust and they'll try to get this argument in without needing me to refactor heaps of PactSwiftMockServer. This should help you pass your headers into your verification and buy me some time to do the refactoring moving off deprecated interfaces.
Unfortunately I'm only working on PactSwift in my spare time.

@kelliepersson
Copy link
Author

@surpher this is awesome! I am glad that this surfaced this issue. I appreciate your work on this and am looking forward to the changes. For someone working in their spare time, you are on it & responsive!!! Thank you so much!

@kelliepersson
Copy link
Author

@surpher wanted to check back in. Has there been any progress from pactffi maintainers, PactSwiftMockServer refactoring? Thanks so much.

@surpher
Copy link
Owner

surpher commented Nov 11, 2022

There's some progress refactoring Verifier using a handle instead of one off trigger on branch feature/provider-verification-upgrade. It has not been tested yet! Literally only created the interface but stopped at creating a test project trying to use it.
Saying that I would very much welcome any feedback on this WIP. Feel free to use what is there.

You should be able to set up provider verification by chaining the commands found on ProviderVerifier object.
Something like the following pseudo code:

func testPactVerification() {
	let verifierHandle = ProviderVerifier(name: "provider", version: "1.0.0")!
	let verificationResult = verifierHandle
		.setCustomHeaders(["Authorization": "Bearer somethingSomething"])
		.setVerificationOptions(disableSSL: true, timeout: 2)
		.setProviderInfo(name: "provider", url: URL(string: "http://localhost:8080")!)
		.verifyPactsAt(source: .file("/tmp/pacts/consumerName-providerName.json"))
		.verify()

	XCTAssertEqual(verificationResult, .success(true))
}

I'm working with the maintainers of the core Pact FFI and we've identified a few things that will need to be resolved as well, particularly when setting provider state setProviderState(url:teardown:body:) which doesn't make much sense in pact_ffi either.

@surpher
Copy link
Owner

surpher commented Nov 28, 2022

@kelliepersson pact_ffi and PactSwiftMockServer has finally been updated to support custom headers for non-handle implementation. Can you give it another whirl using the branch feature/provider_verification_headers_option?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants