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

Resolve relative path #57

Open
fakhrulhilal opened this issue Jan 29, 2021 · 3 comments
Open

Resolve relative path #57

fakhrulhilal opened this issue Jan 29, 2021 · 3 comments
Labels
bug Something isn't working

Comments

@fakhrulhilal
Copy link

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 fakhrulhilal added the bug Something isn't working label Jan 29, 2021
@natenho
Copy link
Owner

natenho commented Jan 31, 2021

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! :)

@fakhrulhilal
Copy link
Author

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?

@natenho
Copy link
Owner

natenho commented Jan 31, 2021

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants