Mockaco icon indicating copy to clipboard operation
Mockaco copied to clipboard

Resolve relative path

Open fakhrulhilal opened this issue 4 years ago • 3 comments

Describe the bug The response.file configuration should be able to resolve relative path instead of absolute path to app root. It's useful when we try to restructure the mocks folder and move the mock files often.

To Reproduce Create this mock under [app]/Mocks folder

{
  "request": {
    "method": "GET",
    "route": "signin"
  },
  "response": {
    "headers": {
      "Content-Type": "text/html; charset=UTF-8"
    },
    "file": "Mocks/signin.html"
  }
}

Assuming signin.html file requires many resource files (images, javascript, css, etc), I grab those from HAR file. Because this page requires many files, so let's structure the folder and create SignIn folder under Mocks folder and move this mock file along with other mocks. Updating from Mocks/signin.html to Mocks/SignIn/signin.html is easy. But updating the rest of the mocks takes time.

Expected behavior Introduce relative path strategy, i.e. ./ or ../ let's assume it's relative to the mock (.json) file

Screenshots

[10:11:33 ERR] 0HM6407H0SUU4:00000011 Error generating mocked response
System.IO.FileNotFoundException: Could not find file 'C:\Application\Mockaco\signin-1.html'.
File name: 'C:\Application\Mockaco\signin.html'
   at System.IO.FileStream.ValidateFileHandle(SafeFileHandle fileHandle)
   at System.IO.FileStream.CreateFileOpenHandle(FileMode mode, FileShare share, FileOptions options)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
   at System.IO.File.ReadAllBytesAsync(String path, CancellationToken cancellationToken)
   at Mockaco.BinaryResponseBodyStrategy.GetFileBytes(String path)
   at Mockaco.BinaryResponseBodyStrategy.GetResponseBodyBytesFromTemplate(ResponseTemplate responseTemplate)
   at Mockaco.ResponseMockingMiddleware.PrepareResponse(HttpResponse httpResponse, Template transformedTemplate, IResponseBodyFactory responseBodyFactory, MockacoOptions options)
   at Mockaco.ResponseMockingMiddleware.Invoke(HttpContext httpContext, IMockacoContext mockacoContext, IScriptContext scriptContext, IResponseBodyFactory responseBodyFactory, IOptionsSnapshot`1 options)
   at Mockaco.RequestMatchingMiddleware.Invoke(HttpContext httpContext, IMockacoContext mockacoContext, IScriptContext scriptContext, IMockProvider mockProvider, ITemplateTransformer templateTransformer, IEnumerable`1 requestMatchers)
   at Mockaco.ResponseDelayMiddleware.Invoke(HttpContext httpContext, IMockacoContext mockacoContext, ILogger`1 logger)
   at Mockaco.ErrorHandlingMiddleware.Invoke(HttpContext httpContext, IMockacoContext mockacoContext, IOptionsSnapshot`1 statusCodeOptions, IMockProvider mockProvider, ILogger`1 logger)

fakhrulhilal avatar Jan 29 '21 03:01 fakhrulhilal

Interesting! It may be a quick fix, would you like to try? It would be great to merge your code here, maybe also giving opinion on the code itself! :)

natenho avatar Jan 31 '21 02:01 natenho

Currently, the file param is relative to Mocks folder. How can we distinguish between relative to Mocks folder and mock file (.json). We have several options here:

  • if it's starter with ./ or ../, then it's relative to json mock file, otherwise, relative to Mocks folder. Cons: inconsistent configuration, need to document that
  • always treat as relative to json mock file, of course, we will support ./ and ../ syntax. Cons: not backward compatible. What do you think?

fakhrulhilal avatar Jan 31 '21 22:01 fakhrulhilal

We can go with the second one

always treat as relative to json mock file, of course, we will support ./ and ../ syntax

but if the file is not found relative to the .json, search it relative to the Mock root path as a fallback, hence keeping backward compatibility.

Although it may cause some confusion in rare cases, I don't think such fallback could cause big problems at all.

Going further: Maybe it could also have a list of "hint paths" just like Visual Studio, a list of paths ordered by priority to search for mocks, when the absolute path is not known (maybe a little too much)

natenho avatar Jan 31 '21 23:01 natenho