LukeMathWalker / wiremock-rs

HTTP mocking to test Rust applications.
Apache License 2.0
617 stars 70 forks source link

Serving test data from a folder #77

Closed mlafeldt closed 2 years ago

mlafeldt commented 2 years ago

Hey,

I recently ported one of my Go apps to Rust. I was happy to discover wiremock along the way, which has served me very well so far. Thanks! 👏

One feature I do miss from Go's httptest package though is the ability to easily serve all test files contained in a folder, e.g.

ts := httptest.NewServer(http.FileServer(http.Dir("testdata")))
defer ts.Close()
// pass ts.URL to tests

With wiremock, the best I could come up with was to read files one by one depending on the test:

  for td in testdata(&server.uri()).iter() {
      let body = fs::read_to_string(format!("dilbert/testdata/strip/{}", td.date)).unwrap();
      let resp = ResponseTemplate::new(200).set_body_raw(body, "text/html");
 ...

However, I didn't like that solution and ended up using include_str instead.

Maybe there's a simpler way I'm missing here?

Or maybe it's a sensible feature request. 😀

LukeMathWalker commented 2 years ago

Hey! Can you share what the files would look like for your Go project? Just to understand better where you are coming from 😁

mlafeldt commented 2 years ago

Sure, for example, https://github.com/mlafeldt/dilbert-feed/tree/main/src/bin/get-strip/testdata contains some HTML files that, when served, mimic dilbert.com.

e.g. /strip/2020-11-11 is the same as https://dilbert.com/strip/2020-11-11

This applies to all files in that folder when testdata is the server's root folder.

$ tree testdata/
testdata/
└── strip
    ├── 2000-01-01
    ├── 2018-10-30
    ├── 2019-11-02
    └── 2020-11-11

1 directory, 4 files
LukeMathWalker commented 2 years ago

Sorry for the late reply, catching up with all things OSS this week 😅 include_str is indeed what I would have recommended - I don't think we are going to provide first-class support in wiremock for the time being.