Wiremock Capture path param and return in the response body












0















I am trying to create dynamic mocks using WireMock. I have a situation where if I specify URL like :



http://localhost:8089/api/account/abc@abc.com


then I should receive response like:



{ 
"account" : "abc@abc.com"
}


In brief, the path param is returned in the response body. I am able to make the request URL generic by using urlPathPattern set to /api/account/([a-z]*) however, I am not sure how should I capture abc@abc.com and return this in the response using regular expressions.










share|improve this question





























    0















    I am trying to create dynamic mocks using WireMock. I have a situation where if I specify URL like :



    http://localhost:8089/api/account/abc@abc.com


    then I should receive response like:



    { 
    "account" : "abc@abc.com"
    }


    In brief, the path param is returned in the response body. I am able to make the request URL generic by using urlPathPattern set to /api/account/([a-z]*) however, I am not sure how should I capture abc@abc.com and return this in the response using regular expressions.










    share|improve this question



























      0












      0








      0








      I am trying to create dynamic mocks using WireMock. I have a situation where if I specify URL like :



      http://localhost:8089/api/account/abc@abc.com


      then I should receive response like:



      { 
      "account" : "abc@abc.com"
      }


      In brief, the path param is returned in the response body. I am able to make the request URL generic by using urlPathPattern set to /api/account/([a-z]*) however, I am not sure how should I capture abc@abc.com and return this in the response using regular expressions.










      share|improve this question
















      I am trying to create dynamic mocks using WireMock. I have a situation where if I specify URL like :



      http://localhost:8089/api/account/abc@abc.com


      then I should receive response like:



      { 
      "account" : "abc@abc.com"
      }


      In brief, the path param is returned in the response body. I am able to make the request URL generic by using urlPathPattern set to /api/account/([a-z]*) however, I am not sure how should I capture abc@abc.com and return this in the response using regular expressions.







      wiremock






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 18 '18 at 12:59









      A. Kootstra

      4,48321032




      4,48321032










      asked Nov 18 '18 at 3:46









      JavaManJavaMan

      568




      568
























          2 Answers
          2






          active

          oldest

          votes


















          1














          In WireMock the regular expressions can be used to recognize the email format in the Request Matching. For the purpose of this example I used a very crude example. Your production implementation may require a more robust approach.



          This request:



          http://localhost:8181/api/account/someone@somewhere.net


          Is matched by this rule:



          {
          "request": {
          "method": "GET",
          "urlPathPattern": "/api/account/([a-z]*@[a-z]*.[a-z]*)"
          },
          "response": {
          "status": 200,
          "jsonBody": {
          "account": "{{request.path.[2]}}"
          },
          "transformers": ["response-template"],
          "headers": {
          "Content-Type": "application/json"
          }
          }
          }


          And returns this response:



          {
          "account": "someone@somewhere.net"
          }


          It makes use of a Response Template processing functionality in WireMock. The Request Model variables [{{request.path.[2]}}] can be used to obtain sections from the request.






          share|improve this answer
























          • Thanks a lot @A. Kootstra for responding to my question. The solution outlined by yourself has worked for me and I am now building more complex tests. Awesome, Cheers mate !!!

            – JavaMan
            Nov 19 '18 at 1:16



















          0














          The same can be done using WireMock.Net - Response-Templating



          The rule looks like:



          {
          "Request": {
          "Path": {
          "Matchers": [
          {
          "Name": "RegexMatcher",
          "Pattern": "^/api/account/([a-z]*@[a-z]*.[a-z]*)$"
          }
          ]
          },
          "Methods": [
          "get"
          ]
          },
          "Response": {
          "StatusCode": 200,
          "BodyAsJson": {
          "account": "{{request.PathSegments.[2]}}"
          },
          "UseTransformer": true,
          "Headers": {
          "Content-Type": "application/json"
          }
          }
          }





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53357707%2fwiremock-capture-path-param-and-return-in-the-response-body%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            In WireMock the regular expressions can be used to recognize the email format in the Request Matching. For the purpose of this example I used a very crude example. Your production implementation may require a more robust approach.



            This request:



            http://localhost:8181/api/account/someone@somewhere.net


            Is matched by this rule:



            {
            "request": {
            "method": "GET",
            "urlPathPattern": "/api/account/([a-z]*@[a-z]*.[a-z]*)"
            },
            "response": {
            "status": 200,
            "jsonBody": {
            "account": "{{request.path.[2]}}"
            },
            "transformers": ["response-template"],
            "headers": {
            "Content-Type": "application/json"
            }
            }
            }


            And returns this response:



            {
            "account": "someone@somewhere.net"
            }


            It makes use of a Response Template processing functionality in WireMock. The Request Model variables [{{request.path.[2]}}] can be used to obtain sections from the request.






            share|improve this answer
























            • Thanks a lot @A. Kootstra for responding to my question. The solution outlined by yourself has worked for me and I am now building more complex tests. Awesome, Cheers mate !!!

              – JavaMan
              Nov 19 '18 at 1:16
















            1














            In WireMock the regular expressions can be used to recognize the email format in the Request Matching. For the purpose of this example I used a very crude example. Your production implementation may require a more robust approach.



            This request:



            http://localhost:8181/api/account/someone@somewhere.net


            Is matched by this rule:



            {
            "request": {
            "method": "GET",
            "urlPathPattern": "/api/account/([a-z]*@[a-z]*.[a-z]*)"
            },
            "response": {
            "status": 200,
            "jsonBody": {
            "account": "{{request.path.[2]}}"
            },
            "transformers": ["response-template"],
            "headers": {
            "Content-Type": "application/json"
            }
            }
            }


            And returns this response:



            {
            "account": "someone@somewhere.net"
            }


            It makes use of a Response Template processing functionality in WireMock. The Request Model variables [{{request.path.[2]}}] can be used to obtain sections from the request.






            share|improve this answer
























            • Thanks a lot @A. Kootstra for responding to my question. The solution outlined by yourself has worked for me and I am now building more complex tests. Awesome, Cheers mate !!!

              – JavaMan
              Nov 19 '18 at 1:16














            1












            1








            1







            In WireMock the regular expressions can be used to recognize the email format in the Request Matching. For the purpose of this example I used a very crude example. Your production implementation may require a more robust approach.



            This request:



            http://localhost:8181/api/account/someone@somewhere.net


            Is matched by this rule:



            {
            "request": {
            "method": "GET",
            "urlPathPattern": "/api/account/([a-z]*@[a-z]*.[a-z]*)"
            },
            "response": {
            "status": 200,
            "jsonBody": {
            "account": "{{request.path.[2]}}"
            },
            "transformers": ["response-template"],
            "headers": {
            "Content-Type": "application/json"
            }
            }
            }


            And returns this response:



            {
            "account": "someone@somewhere.net"
            }


            It makes use of a Response Template processing functionality in WireMock. The Request Model variables [{{request.path.[2]}}] can be used to obtain sections from the request.






            share|improve this answer













            In WireMock the regular expressions can be used to recognize the email format in the Request Matching. For the purpose of this example I used a very crude example. Your production implementation may require a more robust approach.



            This request:



            http://localhost:8181/api/account/someone@somewhere.net


            Is matched by this rule:



            {
            "request": {
            "method": "GET",
            "urlPathPattern": "/api/account/([a-z]*@[a-z]*.[a-z]*)"
            },
            "response": {
            "status": 200,
            "jsonBody": {
            "account": "{{request.path.[2]}}"
            },
            "transformers": ["response-template"],
            "headers": {
            "Content-Type": "application/json"
            }
            }
            }


            And returns this response:



            {
            "account": "someone@somewhere.net"
            }


            It makes use of a Response Template processing functionality in WireMock. The Request Model variables [{{request.path.[2]}}] can be used to obtain sections from the request.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 18 '18 at 13:04









            A. KootstraA. Kootstra

            4,48321032




            4,48321032













            • Thanks a lot @A. Kootstra for responding to my question. The solution outlined by yourself has worked for me and I am now building more complex tests. Awesome, Cheers mate !!!

              – JavaMan
              Nov 19 '18 at 1:16



















            • Thanks a lot @A. Kootstra for responding to my question. The solution outlined by yourself has worked for me and I am now building more complex tests. Awesome, Cheers mate !!!

              – JavaMan
              Nov 19 '18 at 1:16

















            Thanks a lot @A. Kootstra for responding to my question. The solution outlined by yourself has worked for me and I am now building more complex tests. Awesome, Cheers mate !!!

            – JavaMan
            Nov 19 '18 at 1:16





            Thanks a lot @A. Kootstra for responding to my question. The solution outlined by yourself has worked for me and I am now building more complex tests. Awesome, Cheers mate !!!

            – JavaMan
            Nov 19 '18 at 1:16













            0














            The same can be done using WireMock.Net - Response-Templating



            The rule looks like:



            {
            "Request": {
            "Path": {
            "Matchers": [
            {
            "Name": "RegexMatcher",
            "Pattern": "^/api/account/([a-z]*@[a-z]*.[a-z]*)$"
            }
            ]
            },
            "Methods": [
            "get"
            ]
            },
            "Response": {
            "StatusCode": 200,
            "BodyAsJson": {
            "account": "{{request.PathSegments.[2]}}"
            },
            "UseTransformer": true,
            "Headers": {
            "Content-Type": "application/json"
            }
            }
            }





            share|improve this answer




























              0














              The same can be done using WireMock.Net - Response-Templating



              The rule looks like:



              {
              "Request": {
              "Path": {
              "Matchers": [
              {
              "Name": "RegexMatcher",
              "Pattern": "^/api/account/([a-z]*@[a-z]*.[a-z]*)$"
              }
              ]
              },
              "Methods": [
              "get"
              ]
              },
              "Response": {
              "StatusCode": 200,
              "BodyAsJson": {
              "account": "{{request.PathSegments.[2]}}"
              },
              "UseTransformer": true,
              "Headers": {
              "Content-Type": "application/json"
              }
              }
              }





              share|improve this answer


























                0












                0








                0







                The same can be done using WireMock.Net - Response-Templating



                The rule looks like:



                {
                "Request": {
                "Path": {
                "Matchers": [
                {
                "Name": "RegexMatcher",
                "Pattern": "^/api/account/([a-z]*@[a-z]*.[a-z]*)$"
                }
                ]
                },
                "Methods": [
                "get"
                ]
                },
                "Response": {
                "StatusCode": 200,
                "BodyAsJson": {
                "account": "{{request.PathSegments.[2]}}"
                },
                "UseTransformer": true,
                "Headers": {
                "Content-Type": "application/json"
                }
                }
                }





                share|improve this answer













                The same can be done using WireMock.Net - Response-Templating



                The rule looks like:



                {
                "Request": {
                "Path": {
                "Matchers": [
                {
                "Name": "RegexMatcher",
                "Pattern": "^/api/account/([a-z]*@[a-z]*.[a-z]*)$"
                }
                ]
                },
                "Methods": [
                "get"
                ]
                },
                "Response": {
                "StatusCode": 200,
                "BodyAsJson": {
                "account": "{{request.PathSegments.[2]}}"
                },
                "UseTransformer": true,
                "Headers": {
                "Content-Type": "application/json"
                }
                }
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 21 '18 at 11:55









                Stef HeyenrathStef Heyenrath

                4,24184483




                4,24184483






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53357707%2fwiremock-capture-path-param-and-return-in-the-response-body%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    MongoDB - Not Authorized To Execute Command

                    How to fix TextFormField cause rebuild widget in Flutter

                    in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith