Testing Caddyfile parsing

I wanted to test the code in my plugin that parses the Caddy file. However, how the parsing works is one gets a httpcaddyfile.Helper with just the contents of your module. I would need a way to mock this h httpcaddyfile.Helper with some sample Caddyfile configs for my plugin.

Is there anyway to do this? Any suggestions welcome!

The ideal solution would be a helper function that can take a partial Caddy file and turn it into a httpcaddyfile.Helper I could use to test against. I have not dug in enough to know if that is at all easy or hard…

Great question – I think there’s been some discussion about this on the issue tracker already. Aha, yes, here it is: caddyfile: Make NewTestDispenser accessible for plugin authors by francislavoie · Pull Request #3439 · caddyserver/caddy · GitHub

Is that what you’re looking for?

Close! The function I’m testing takes a httpcaddyfile.Helper though. I’m trying to figure out how I can convert the Dispenser into a Helper…


Well - I got it work. But I had to refactor my caddyfileParser function to be able to call it with a Dispenser. This kind of makes the code a little weird - but not too bad.

It would be better if we could get a mock Helper like I can get the mock Dispenser.

However, it works and writing the tests certainly caused me to fix a couple of issues - so I very much recommend folks do this!

1 Like

Cool. Would you be able to submit a PR with such a helper function? I’m a bit tied up this next week.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.