Kong / go-pdk

Write Kong plugins in Go! 🦍
https://pkg.go.dev/github.com/Kong/go-pdk
Apache License 2.0
144 stars 48 forks source link

How to set a variable to Kong Context while doing unit tests? #171

Closed lays147 closed 4 months ago

lays147 commented 6 months ago

Hi, I'm trying to write a couple of unit tests for a plugin that I'm doing, and based on the docs, I can't see where I could add a variable to the kong.context for the purpose of the unit test.

Checking the source code, the Ctx is available at env.pdk, but this variable is not available for external access.

Water-Melon commented 5 months ago

https://github.com/Kong/go-pdk/blob/master/ctx/ctx.go#L19

nowNick commented 5 months ago

Hey! @lays147

Thank you for sending your question! Does the link that @Water-Melon sent above help (☝️)? There's some documentation that should explain how to use kong.ctx (context). Would that work for your use case?

lays147 commented 5 months ago

Hi @nowNick no. As I said, the context var on the test pdk is not available. I can use it on writing a plugin, but I can't manage the context on the text setup. Because in this case, I'm retrieving a var from the context, not setting it.

Also, the docs are wrong because to use SetShared requires a key and value, and the pdk docs shows otherwise. Moreover, it does not make sense to save something in context without a value, only with a key.

nowNick commented 5 months ago

Oh! Ok I see. Apologies for the confusion then!

I'll take a deeper look at this and I'll try to offer better guidance / report bugs.

winslowdibona commented 5 months ago

@nowNick I was running into the same issue and created PR #174 which would allow the usage of the shared context during testing.

nowNick commented 5 months ago

That's awesome @winslowdibona! Thank you for your contribution!

winslowdibona commented 4 months ago

@nowNick Is there someone specific I should reach out to about reviewing PR #174 ? I've mentioned a few others on there but I haven't gotten any reviews/comments on it in about a week.

nowNick commented 4 months ago

Hey @winslowdibona If possible I'd like to merge this one first: https://github.com/Kong/go-pdk/pull/170 and then rebase your PR on top of it. I'll keep track of both of these PRs to make sure they're merged swiftly.

gszr commented 4 months ago

Solved by #174