smocker-dev / smocker

Smocker is a simple and efficient HTTP mock server and proxy
https://smocker.dev
MIT License
1.13k stars 59 forks source link

How do mocks and sessions relate? #293

Closed BigNerd closed 3 months ago

BigNerd commented 5 months ago

The smocker documentation, for example https://smocker.dev/technical-documentation/api.html#add-mocks suggests that mocks are created and maintained with respect to a session. Can you please clarify how this works and what it is useful for? When any request is sent to smocker against the mock server port (8080), it has no link to a session, so how can mock definitions be scoped under a session? Is it not rather that mocks are in the global scope and can be used by any session?

Thiht commented 5 months ago

Sessions in Smocker are used to group a series of calls in a logical unit. For example, we make great use of sessions in our test suites: 1 session = 1 test suite or 1 test case (depending on the granularity we want). This means at the beginning of each test suite, we create a new session to add mocks into (or directly add the mocks to a new session via ?session=), and then run our tests. This means after executing all our test suites, everything is neatly grouped in a session, so if a test fails we just need to check its corresponding session in Smocker to see if something went wrong because of an HTTP call.

When any request is sent to smocker against the mock server port (8080), it has no link to a session, so how can mock definitions be scoped under a session?

Once a new session is created, the previous sessions become readonly, they're just here for "history" purpose. That's why any mocks you create are implicitly created under the currently active session, i.e. the last created (unless a new session is explicitly specified when creating the mocks).

Is it not rather that mocks are in the global scope and can be used by any session

When you create a new session, it doesn't inherit the mocks from the previous session(s)

(there might be an exception for locked mocks but I don't think so, I don't remember how we implemented its)

Hope it clarifies how sessions work and what you can do with them! If you don't need a session mechanism, you can ignore it and work only with the session that's implicitly created when you add new mocks, and use it as a global session

gwleclerc commented 5 months ago

(there might be an exception for locked mocks but I don't think so, I don't remember how we implemented its)

Yes Locked mocks are duplicated and reseted if you start a new session or reset them.

BigNerd commented 5 months ago

Thank you very much for the explanation! I believe to understand that everytime a new session is created it is not inheriting the mocks from the previously active session(s) except if they had been locked. There is only ever one active session and only the mocks associated with it are active and can be used for the requests coming into the mock server port. Please let me know if I still misunderstand, but otherwise I am now clear with it and can make good use of it.

gwleclerc commented 5 months ago

That's exactly it.