From 3c28cf2960e19b066a7c63c0cc01152071dcb2b7 Mon Sep 17 00:00:00 2001 From: Oleg Nenashev Date: Mon, 4 Sep 2023 15:25:10 +0200 Subject: [PATCH 1/5] Initial adaptation of the Jekyll code tabs --- _docs/request-matching.md | 87 +++++++++++++++++++++++---------- _layouts/docs.html | 72 +++++++++++++++++++++++++++ _plugins/jekyll-code-tabs.rb | 1 + lib/jekyll-code-tabs.rb | 58 ++++++++++++++++++++++ lib/jekyll-code-tabs/version.rb | 5 ++ 5 files changed, 197 insertions(+), 26 deletions(-) create mode 100644 _plugins/jekyll-code-tabs.rb create mode 100644 lib/jekyll-code-tabs.rb create mode 100644 lib/jekyll-code-tabs/version.rb diff --git a/_docs/request-matching.md b/_docs/request-matching.md index 7e59de01..f7ce34fe 100644 --- a/_docs/request-matching.md +++ b/_docs/request-matching.md @@ -8,21 +8,26 @@ description: WireMock supports matching of requests to stubs and verification qu WireMock enables flexible definition of a [mock API](/) by supporting rich matching of incoming requests. Stub matching and verification queries can use the following request attributes: -- URL -- HTTP Method -- Query parameters -- Form parameters -- Headers -- Basic authentication (a special case of header matching) -- Cookies -- Request body -- Multipart/form-data +- URL +- HTTP Method +- Query parameters +- Form parameters +- Headers +- Basic authentication (a special case of header matching) +- Cookies +- Request body +- Multipart/form-data Here's an example showing all attributes being matched using WireMock's in-built match operators. It is also possible to write [custom matching logic](../extending-wiremock#custom-request-matchers) if you need more precise control: ## Request with XML Body -Java: + +Code: + +{% codetabs %} + +{% codetab Java %} ```java stubFor(any(urlPathEqualTo("/everything")) @@ -41,7 +46,31 @@ stubFor(any(urlPathEqualTo("/everything")) .willReturn(aResponse())); ``` -JSON: +{% endcodetab %} + +{% codetab Python %} + +```python +print("Hello, world!") +``` + +{% endcodetab %} + +{% codetab Go %} + +```golang +fmt.Println("Hello, world!") +``` + +{% endcodetab %} + +{% endcodetabs %} + +Configuration file: + +{% codetabs %} + +{% codetab JSON %} ```json { @@ -100,6 +129,14 @@ JSON: } ``` +{% endcodetab %} + +{% codetab YAML %} + +{% endcodetab %} + +{% endcodetabs %} + ## Request with Form Parameters ```java @@ -126,7 +163,6 @@ stubFor(post(urlPathEqualTo("/mock")) } ``` - The following sections describe each type of matching strategy in detail. ## URL matching @@ -664,7 +700,7 @@ JSON: Request body example: -``` +```json // matching { "things": { "name": "RequiredThing" } } { "things": [ { "name": "Required" }, { "name": "Wiremock" } ] } @@ -701,7 +737,7 @@ JSON: Request body example: -``` +```json // matching { "things": [ { "name": "RequiredThing" }, { "name": "Wiremock" } ] } // not matching @@ -1340,14 +1376,14 @@ JSON:
The full list of available truncations is: -- `first minute of hour` -- `first hour of day` -- `first day of month` -- `first day of next month` -- `last day of month` -- `first day of year` -- `first day of next year` -- `last day of year` +- `first minute of hour` +- `first hour of day` +- `first day of month` +- `first day of next month` +- `last day of month` +- `first day of year` +- `first day of next year` +- `last day of year` ## Logical AND and OR @@ -1487,7 +1523,6 @@ This would match the following JSON request body: } ``` - ### Matching Header/Query parameter containing multiple values You can match multiple values of a query parameter or header with below provided matchers. @@ -1501,7 +1536,7 @@ stubFor(get(urlPathEqualTo("/things")) .willReturn(ok())); ``` -```json +```json { "mapping": { "request" : { @@ -1607,7 +1642,6 @@ stubFor(get(urlPathEqualTo("/things")) } ``` - ```java //values of id must conform to the match expressions stubFor(get(urlPathEqualTo("/things")) @@ -1617,6 +1651,7 @@ stubFor(get(urlPathEqualTo("/things")) notContaining("3") )).willReturn(ok())); ``` + ```json { "mapping": { @@ -1644,4 +1679,4 @@ stubFor(get(urlPathEqualTo("/things")) } } } -``` \ No newline at end of file +``` diff --git a/_layouts/docs.html b/_layouts/docs.html index 67f257d9..e28c509d 100644 --- a/_layouts/docs.html +++ b/_layouts/docs.html @@ -29,7 +29,79 @@ visibility: visible; } + + /* Code tabs inspired by https://github.com/clustergarage/jekyll-code-tabs/pull/5 */ + .tab { + overflow: hidden; + border: 1px solid #ccc; + background-color: #f1f1f1; + } + + .tab button { + background-color: inherit; + float: left; + border: none; + outline: none; + cursor: pointer; + padding: 14px 16px; + transition: 0.3s; + } + + .tab button:hover { + background-color: #ddd; + } + + .tab button.active { + background-color: #ccc; + } + + .tabcontent { + display: none; + padding: 6px 12px; + border: 1px solid #ccc; + border-top: none; + } + + .tabcontent { + animation: fadeEffect 0.5s; /* Fading effect takes 1 second */ + } + + @keyframes fadeEffect { + from {opacity: 0.5;} + to {opacity: 1;} + } + + + +
diff --git a/_plugins/jekyll-code-tabs.rb b/_plugins/jekyll-code-tabs.rb new file mode 100644 index 00000000..6b36d369 --- /dev/null +++ b/_plugins/jekyll-code-tabs.rb @@ -0,0 +1 @@ +require_relative "../lib/jekyll-code-tabs" diff --git a/lib/jekyll-code-tabs.rb b/lib/jekyll-code-tabs.rb new file mode 100644 index 00000000..bdb79c76 --- /dev/null +++ b/lib/jekyll-code-tabs.rb @@ -0,0 +1,58 @@ +require "erb" +require 'securerandom' +require_relative "jekyll-code-tabs/version" + +module Jekyll + module CodeTabs + class CodeTabsBlock < Liquid::Block + def render(context) + environment = context.environments.first + environment['codetabs'] = {} # reset each time + super + + uuid = SecureRandom.uuid + template = ERB.new <<-EOF + +
+
+ <% environment['codetabs'].each_with_index do |(key, _), index| %> + + <% end %> +
+ + + <% environment['codetabs'].each do |key, value| %> +
+ <%= value %> +
+ <% end %> +
+EOF + template.result(binding) + end + end + + class CodeTabBlock < Liquid::Block + alias_method :render_block, :render + + def initialize(tag_name, markup, tokens) + super + if markup == "" + raise SyntaxError.new("No toggle name given in #{tag_name} tag") + end + @toggle = markup.strip + end + + def render(context) + site = context.registers[:site] + converter = site.find_converter_instance(::Jekyll::Converters::Markdown) + environment = context.environments.first + environment['codetabs'] ||= {} + environment['codetabs'][@toggle] = converter.convert(render_block(context)) + end + end + end +end + +Liquid::Template.register_tag("codetab", Jekyll::CodeTabs::CodeTabBlock) +Liquid::Template.register_tag("codetabs", Jekyll::CodeTabs::CodeTabsBlock) diff --git a/lib/jekyll-code-tabs/version.rb b/lib/jekyll-code-tabs/version.rb new file mode 100644 index 00000000..4cfd2fab --- /dev/null +++ b/lib/jekyll-code-tabs/version.rb @@ -0,0 +1,5 @@ +module Jekyll + module CodeTabs + VERSION = "1.2.0-patched".freeze + end +end From 821ed4013822d83daa80bcae768610cf9c77f945 Mon Sep 17 00:00:00 2001 From: Oleg Nenashev Date: Mon, 4 Sep 2023 15:30:28 +0200 Subject: [PATCH 2/5] Fix appearance of the default code tabs --- _layouts/docs.html | 5 ++++- lib/jekyll-code-tabs.rb | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/_layouts/docs.html b/_layouts/docs.html index e28c509d..967da6be 100644 --- a/_layouts/docs.html +++ b/_layouts/docs.html @@ -76,7 +76,10 @@ diff --git a/lib/jekyll-code-tabs.rb b/lib/jekyll-code-tabs.rb index d9831f88..e79190a9 100644 --- a/lib/jekyll-code-tabs.rb +++ b/lib/jekyll-code-tabs.rb @@ -16,8 +16,9 @@ def render(context)
<% environment['codetabs'].each_with_index do |(key, _), index| %> - + <% end %> +
From d9d2a8a1d57e4073bc558336dd650306cf0279dd Mon Sep 17 00:00:00 2001 From: Oleg Nenashev Date: Mon, 4 Sep 2023 16:27:11 +0200 Subject: [PATCH 4/5] Remove test code tabs --- _docs/request-matching.md | 36 ------------------------------------ 1 file changed, 36 deletions(-) diff --git a/_docs/request-matching.md b/_docs/request-matching.md index f7ce34fe..58795547 100644 --- a/_docs/request-matching.md +++ b/_docs/request-matching.md @@ -25,10 +25,6 @@ you need more precise control: Code: -{% codetabs %} - -{% codetab Java %} - ```java stubFor(any(urlPathEqualTo("/everything")) .withHeader("Accept", containing("xml")) @@ -46,32 +42,8 @@ stubFor(any(urlPathEqualTo("/everything")) .willReturn(aResponse())); ``` -{% endcodetab %} - -{% codetab Python %} - -```python -print("Hello, world!") -``` - -{% endcodetab %} - -{% codetab Go %} - -```golang -fmt.Println("Hello, world!") -``` - -{% endcodetab %} - -{% endcodetabs %} - Configuration file: -{% codetabs %} - -{% codetab JSON %} - ```json { "request": { @@ -129,14 +101,6 @@ Configuration file: } ``` -{% endcodetab %} - -{% codetab YAML %} - -{% endcodetab %} - -{% endcodetabs %} - ## Request with Form Parameters ```java From 5edecae2583e4932a9652dfb05fad5cbff4d2ccd Mon Sep 17 00:00:00 2001 From: Oleg Nenashev Date: Mon, 4 Sep 2023 16:48:20 +0200 Subject: [PATCH 5/5] Add real example of multiple code tabs to /stubbing --- _docs/stubbing.md | 90 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 65 insertions(+), 25 deletions(-) diff --git a/_docs/stubbing.md b/_docs/stubbing.md index 90a9c8bb..6d4fe204 100644 --- a/_docs/stubbing.md +++ b/_docs/stubbing.md @@ -12,11 +12,44 @@ responses for requests matching criteria. These are described in detail in [Requ ## Basic stubbing -The following code will configure a response with a status of 200 to be -returned when the relative URL exactly matches `/some/thing` (including -query parameters). The body of the response will be "Hello world!" and a +You can configure stubs using JSON configuration files or code: + +1. Via a `.json` file under the `mappings` directory +2. Via a POST request to +`http://:/__admin/mappings` with the JSON as a body +3. From code using one of the SDKs + +**Example.** +To configure a response with a status of 200 to be returned +when the relative URL exactly matches `/some/thing` (including query parameters). +The body of the response will be "Hello world!" and a `Content-Type` header will be sent with a value of `text-plain`. +{% codetabs %} + +{% codetab JSON %} + +```json +{ + "request": { + "method": "GET", + "url": "/some/thing" + }, + + "response": { + "status": 200, + "body": "Hello, world!", + "headers": { + "Content-Type": "text/plain" + } + } +} +``` + +{% endcodetab %} + +{% codetab Java %} + ```java @Test public void exactUrlOnly() { @@ -30,32 +63,39 @@ public void exactUrlOnly() { } ``` -> **note** -> -> If you'd prefer to use slightly more BDDish language in your tests you -> can replace `stubFor` with `givenThat`. +{% endcodetab %} -To create the stub described above via the JSON API, the following -document can either be posted to -`http://:/__admin/mappings` or placed in a file with a -`.json` extension under the `mappings` directory: +{% codetab Python %} -```json -{ - "request": { - "method": "GET", - "url": "/some/thing" - }, - "response": { - "status": 200, - "body": "Hello world!", - "headers": { - "Content-Type": "text/plain" - } - } -} +```python +Mappings.create_mapping( + Mapping( + request=MappingRequest(method=HttpMethods.GET, url="/some/thing"), + response=MappingResponse(status=200, body="Hello, world!", headers=("Content-Type", "text/plain")), + ) +) +``` + +{% endcodetab %} + +{% codetab Golang %} + +```go +wiremockClient.StubFor(wiremock.Get(wiremock.URLPathEqualTo("/some/thing")). + WillReturnResponse( + wiremock.NewResponse(). + WithStatus(http.StatusOK). + WithBody("Hello, world!"). + WithHeader("Content-Type", "text/plain"))) ``` +{% endcodetab %} + +{% endcodetabs %} + +In Java, if you'd prefer to use slightly more BDDish language in your tests, +you can replace `stubFor` with `givenThat`. + ### Java Shortcuts Some common request and response patterns can be expressed in Java in abbreviated forms.