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

Scripting doesn't work for file template #58

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

Scripting doesn't work for file template #58

fakhrulhilal opened this issue Jan 29, 2021 · 6 comments
Labels
enhancement New feature or request

Comments

@fakhrulhilal
Copy link

Describe the bug
When sending response from external file (f.e. html file) containing script, the script is not evaluated

To Reproduce
Using these mocks:

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

and

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

And this's html file

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
<form method="post">
    <label for="username">Username</label>: 
    <input type="text" name="username" id="username" value="<#= Request.Body["username"]?.ToString() #>"/>
</form>
</body>
</html>

Expected behavior
The script is evaluated

Screenshots
mockaco-scripting

@fakhrulhilal fakhrulhilal added the bug Something isn't working label Jan 29, 2021
@natenho
Copy link
Owner

natenho commented Jan 31, 2021

It's a good use case. Originally, the intent of that feature was to provide raw content, like images, binary stuff. Maybe we could add some flag to indicate that the file should be parsed as script:

{
  "request": {
    "route": "login",
    "method": "GET"
  },
  "response": {
    "headers": {
      "Content-Type": "text/html; charset=UTF-8"
    },
    "status": "OK",
    "file": {
      "path": "Mocks/login.html",
      "containsScript": true
    }
  }
}

Obviously I would keep the "file": "path" as an alternate syntax backward compatible.

What do you think?

@fakhrulhilal
Copy link
Author

Previously, I thought scripting works for every template. I think that's good option.

@tkrafael
Copy link

Maybe the script engine can use razor extension (cshtml) to determine if it should run it or not.

@natenho natenho added enhancement New feature or request and removed bug Something isn't working labels Mar 4, 2021
@gomete
Copy link
Contributor

gomete commented Mar 18, 2022

Hi @natenho,

Any plan to implement this? It would be a nice feature. It is easiest to have XML responses for example in an external file.

wiremock.org for example, divides the mocks folder (by default mappings) and the files folder (by default __files). Have you considered something like this? In case of extracting JSON responses having them in the same folder will make that the MockProvider will try to read them as mocks, am I right?

@natenho
Copy link
Owner

natenho commented Mar 20, 2022

@gomete

  1. I agree it is a good enhancement, but no, I was considering to fix High memory usage at startup #82 before planning to implement any new feature (to be honest I realized that working alone with this increasing backlog is not easy, so as more contributors like you come in, we need to organize a decent roadmap)

  2. Yes, any json file would be read as mocks in that case so the current working options are:

  • Place them in .mockignore file (it is supposed to work just like .gitignore)
  • Change their extension to another one different from .json

I prefer not having any folder convention for the sake of simplicity, what if we add a logic to "do not consider dependent files as mocks"?? i.e. if a given file is referenced inside another mock, it should not be considered as a mock itself

@gomete
Copy link
Contributor

gomete commented Mar 20, 2022

Great, I wasn't considering the .mockignore but that will work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants