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

Providing path to json file instead of content of the json. #60

Open
bartzet opened this issue Jun 14, 2022 · 1 comment
Open

Providing path to json file instead of content of the json. #60

bartzet opened this issue Jun 14, 2022 · 1 comment

Comments

@bartzet
Copy link

bartzet commented Jun 14, 2022

Instead of providing the JSON format structure after return, is there a way to point Ambassador to the .json file located in the project of the app we want to test?

instead of

router["/api/v2/users"] = JSONResponse() { _ -> Any in
    return [
        ["id": "01", "name": "john"],
        ["id": "02", "name": "tom"]
    ]
}

something like:

router["/api/v2/users"] = JSONResponse() { _ -> Any in
    return [
       path/to/file/users.json
    ]
}
@shero-investing
Copy link

yup, I added a helper function like

static func loadJSONResponseFromFile(_ fileName: String) -> Any {
        let bundle = Bundle(for: self)
        if let path = bundle.path(forResource: fileName, ofType: "json"),
           let data = try? Data(contentsOf: URL(fileURLWithPath: path)) {
            return try! JSONSerialization.jsonObject(with: data, options: [])
        }
        return ["error": "File not found"]
    }

and calling it like

router["/myPath"] = JSONResponse(handler: { environ in
            loadJSONResponseFromFile("myFileName")
        })

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

No branches or pull requests

2 participants