-
Notifications
You must be signed in to change notification settings - Fork 18
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
Returning images as mock #31
Comments
Nice idea |
I still can't mock images. Can anyone give me a better idea to fix? Here is my configuration of mocking : And i have tried for http://localhost:8080/t/p/images/w342/2CAL2433ZeIihfX1Hb2139CX0pW.jpg but it return 404 not found page |
Image responses are not implemented yet. But you can send an image as a response in a custom response. So, if you put a custom middleware in your const fs = require('fs');
const path = require('path');
module.exports = (req, res) => {
// Assume that we have a GET.jpg file next to this file
// You can also use `req.params.filename` as file name, if you want to send a dynamic image
const filePath = path.join(__dirname, 'GET.jpg');
const stat = fs.statSync(filePath);
// Set response type as image and content-length
res.writeHead(200, {
'Content-Type': 'image/jpeg',
'Content-Length': stat.size
});
// Read file and pipe it to response
const readStream = fs.createReadStream(filePath);
readStream.pipe(res);
} I didn't try it but I hope that will give the idea. |
Yes. I have find a solution (nearly your response). I have using GET.js rather than GET.jpg and sends a image through GET.js Here is screenshot of my files (a little) And its my GET.js file When i starts a dev-server and then implement the endpoint. Its going works... Thanks 👍 👍 👍 |
I just added 2 new helper functions( const { type, file } = require('connect-api-mocker/helpers');
// Assuming a file named GET.png exists next to this file
const filePath = path.join(__dirname, './img.jpg');
module.exports = [type('image/jpeg'), file(filePath)]; But still, I want to implement the idea that I mentioned in first comment. I think it would be more elegant solution. |
Yes sure. But the issue is coming again when the endpoint (to get images) is not found. And redirected to webpack-dev-server proxy. I think this is problem of proxy or my bad webpack configuration (Iam new to webpack :( ) |
For some use cases, we need to able to return image responses as mocks. Like
GET.json
orGET.xml
,GET.jpeg
should be returned for an image request. There are some tricky parts for this:auto
type that is checking request content-type header and falls-back to json. But for images we should check mock files for this path in any case for image mock files.So idea is:
GET /path
with accept typeapplication/json
we should only checkpath/GET.json
* Custom middlewares should have priority in any case
** If there is no prefered accept type and if there is a json mock file, it should be prioritised
The text was updated successfully, but these errors were encountered: