Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wiremock - WatchStaticMappings only works until the first request is made #726

Closed
jucamo8311 opened this issue Feb 17, 2022 · 12 comments
Closed
Labels

Comments

@jucamo8311
Copy link

Describe the bug

I created a standalone console app to mock requests using WireMock.Net.StandAlone library , the configuration of the server is like below

var settings = new WireMockServerSettings()
            {
                AllowBodyForAllHttpMethods = true,
                UseSSL = true,
                Urls = new[] {
                    string.Format("http:https://localhost:{0}",HttpPort),
                    string.Format("https://localhost:{0}",HttpsPort)
                    },
                MaxRequestLogCount = 100,
                Logger = new CustomLocalLogger(),
                AllowCSharpCodeMatcher = true,
                CertificateSettings = new WireMockCertificateSettings()
                {
                    X509CertificateFilePath = "######.pfx",
                    X509CertificatePassword = "######"
                },

            };
var server = StandAloneApp.Start(settings);
server.ReadStaticMappings(MappingsFolder);
server.WatchStaticMappings(MappingsFolder)
Console.WriteLine("Press any key to stop the server");
Console.ReadKey();
server.Stop();

Expected behavior:

I believe the correct behavior is to detect changes on MappingsFolder an update the mapping accordingly. In some way it seems to work before is made the first request, after that it’s not able to detect changes on the json mapping files (update , delete , add).

Test to reproduce

  • 1 Create a console application and start a server using WireMock.Net.StandAlone library
  • 2 Create a json mapping file and put it on a folder and set the value for ReadStaticMappings and WatchStaticMappings
  • 3 Modify the mapping file to see that the changes are made.
  • 4 Test the mock making a request.
  • 5 Modify again the mapping file(e.g. changing the response ).
  • 6 Test again the same mock to see that the change is not present in the response

Other related info

  • App build on .net core 5.0
  • WireMock.Net Version 1.4.35
  • Test on windows server 2012

Thanks, in Avance for your time, wiremock.net is a great library

@jucamo8311 jucamo8311 added the bug label Feb 17, 2022
@StefH
Copy link
Collaborator

StefH commented Feb 18, 2022

@jucamo8311
I see you have a custom logger : CustomLocalLogger, can you please check this logging and copy-past that here in this issue?

(A possible reason that it does not work is an invalid json file.)

See

@jucamo8311
Copy link
Author

I use serilog for the log (the serilog MinimumLevel is set to dedug in order to print everything ) , checking this log what I can see is

2022-02-18 11:29:37 [Information] => Server using .NET 5.0.
2022-02-18 11:29:37 [Information] => AllowBodyForAllHttpMethods is set to True.
2022-02-18 11:29:37 [Information] => Version ["1.4.35.0"].
2022-02-18 11:29:37 [Information] => Server listening at "http:https://localhost:9080,https://localhost:9043".
2022-02-18 11:29:37 [Information] => Reading Static MappingFile : '"F:\MockData\Mappings\SalesforceCRM.json"'.
2022-02-18 11:29:37 [Information] => Watching folder 'F:\MockData\Mappings' for new, updated and deleted MappingFiles..

Here I change the response in the mapping file SalesforceCRM.json, from this

[{
        "Guid": "05E2F022-C4B2-4406-9E31-E921DE415BF5",
        "priority": 10,
        "Request": {
            "Path": {
                "Matchers": [{
                        "Name": "WildcardMatcher",
                        "Pattern": "/services/apexrest/ConsultarCaso*",
                        "IgnoreCase": true
                    }
                ]
            },
            "Headers": [{
                    "Name": "Authorization",
                    "Matchers": [{
                            "Name": "WildcardMatcher",
                            "Pattern": "Bearer*",
                            "IgnoreCase": true
                        }
                    ]
                }
            ],
            "Methods": ["get"]
        },
        "Response": {
            "Headers": {
                "Content-Type": "application/json;charset=UTF-8",
                "X-Robots-Tag": "none",
                "X-Content-Type-Options": "nosniff",
                "Strict-Transport-Security": "max-age=31536000; includeSubDomains",
				"X-XSS-Protection":"1; mode=block"
            },
            "BodyAsFile": "F:\\MockData\\filesResponses\\SalesforceCRM\\Test_Resp_OK_1.json",
            "UseTransformerForBodyAsFile": true,
            "UseTransformer": true
        }
    }
]
  • to this (the only change is the value on the paramater BodyAsFile from Test_Resp_OK_1.json to Test_Resp_OK__2.json )
[{
        "Guid": "05E2F022-C4B2-4406-9E31-E921DE415BF5",
        "priority": 10,
        "Request": {
            "Path": {
                "Matchers": [{
                        "Name": "WildcardMatcher",
                        "Pattern": "/services/apexrest/ConsultarCaso*",
                        "IgnoreCase": true
                    }
                ]
            },
            "Headers": [{
                    "Name": "Authorization",
                    "Matchers": [{
                            "Name": "WildcardMatcher",
                            "Pattern": "Bearer*",
                            "IgnoreCase": true
                        }
                    ]
                }
            ],
            "Methods": ["get"]
        },
        "Response": {
            "Headers": {
                "Content-Type": "application/json;charset=UTF-8",
                "X-Robots-Tag": "none",
                "X-Content-Type-Options": "nosniff",
                "Strict-Transport-Security": "max-age=31536000; includeSubDomains",
				"X-XSS-Protection":"1; mode=block"
            },
            "BodyAsFile": "F:\\MockData\\filesResponses\\SalesforceCRM\\Test_Resp_OK_2.json",
            "UseTransformerForBodyAsFile": true,
            "UseTransformer": true
        }
    }
]

After that I can see in the log that a message was printed

2022-02-18 11:30:29 [Information] => MappingFile updated : '"F:\MockData\Mappings\SalesforceCRM.json"', reading file..

Making a call to the mock using postman, the response is the content of Test_Resp_OK_2.json.

if I modify the file SalesforceCRM.json again changing the response file from Test_Resp_OK_2.json to Test_Resp_OK_1.json
nothing in the log is printed , and if I test the mock is still returning Test_Resp_OK_2.json

@StefH
Copy link
Collaborator

StefH commented Feb 18, 2022

It's very strange.

In .NET old (4.5.2) and .NETCore 3.1, it works / worked fine.

For .NET 5.0, is does not always work correct.

I did a small fix, can you try preview:

1.4.35-ci-15874

https://github.com/WireMock-Net/WireMock.Net/wiki/MyGet-preview-versions

@StefH
Copy link
Collaborator

StefH commented Feb 21, 2022

Maybe related to?
#729

@jucamo8311
Copy link
Author

I checked that build but it behaves the same, I starting to think that maybe it is not a problem of the library, more of .net core framework itself, because testing I noticed something even weirder. On Windows 10 if I execute my application on a console and end the application it is necessary to close the windwos console itself in order to take the changes I made on the mapping file, which made no sense because the app ended and should take the update version of the file when I start it again.

Anyway, tomorrow I am going to check BodyAsFileIsCached to see if make any diference

@StefH
Copy link
Collaborator

StefH commented Feb 25, 2022

New version (x.x.x.36) will be available on NuGet within some hours.

Maybe can you retest that version.

And maybe did you try this:
https://github.com/WireMock-Net/WireMock.Net/blob/master/src/WireMock.Net/Settings/WireMockServerSettings.cs#L44

@jucamo8311
Copy link
Author

I checked it, but nothing change. It doesn't take the mapping files update. Also I made a test with Framework 4.7.2 and like you said It works. So my conclucion is that something related to .net core framework doesnt work as expected

@StefH
Copy link
Collaborator

StefH commented Feb 28, 2022

@StefH
Copy link
Collaborator

StefH commented Mar 1, 2022

@jucamo8311
I think I found it.

Can you try preview:
1.4.36-ci-15935

@jucamo8311
Copy link
Author

Yes , that does it ,

Tested with .Net core 6.0 win 10 x64.
Tested with .Net core 6.0 win 2012 R2 x64.

@StefH
Copy link
Collaborator

StefH commented Mar 1, 2022

This bug has been present for some time now.

Thanks for finding this!

@StefH
Copy link
Collaborator

StefH commented Mar 1, 2022

#733

@StefH StefH closed this as completed Mar 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants