Skip to content

Instantly share code, notes, and snippets.

View WaleedAshraf's full-sized avatar
:octocat:
Focusing

Waleed Ashraf WaleedAshraf

:octocat:
Focusing
View GitHub Profile
@WaleedAshraf
WaleedAshraf / cookies.md
Last active March 21, 2023 08:42
Mocking Express Session For Tests

How to mock express session?

sinon is being used to mock methods in tests. It is one of the most used package for testing. But Is there a way to mock session objects in tests for express?

I'm working on a project where I upload images to s3 in bulk (albums) and than further apply some processing like resizing/compression/editing on them.

It's a express app with middlewares for authorization, authentication, setting context and is working fine. Recently I started adding testcases for different routes using mocha. I'm using cookie-session for session handling in app.

For one route, I needed to mock req.session object. (Need to set it's value before calling the other route.)

@WaleedAshraf
WaleedAshraf / serverless-app-4.xml
Created April 19, 2018 13:02
Building you first Serverless app in Node.js with AWS Lambda + S3 + API Gateway
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>PUT</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>HEAD</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>Authorization</AllowedHeader>
asyncapi: '2.0.0-rc2'
id: 'urn:demo'
info:
title: Demo
version: '1.0'
channels:
userEvents:
description: User Events Channel
publish:
summary: Publish User related events
@WaleedAshraf
WaleedAshraf / dns-sync.sh
Created August 19, 2019 14:41 — forked from matthiassb/dns-sync.sh
Init.d script for keeping WSL resolv.conf in-sync with Windows
#! /bin/bash
### BEGIN INIT INFO
# Provides: dns-sync
# Required-Start:
# Required-Stop:
# Default-Start: S
# Default-Stop:
# Short-Description: Synchronizes /etc/resolv.conf in WLS with Windows DNS - Matthias Brooks
### END INIT INFO
@WaleedAshraf
WaleedAshraf / cluster-module.js
Last active October 31, 2018 02:50
Switching from cluster module to PM2 & RabbitMQ
var cluster = require('cluster');
const numCPUs = require('os').cpus().length;
module.exports.create = function(options, callback){
if (cluster.isMaster) {
// fork child process for notif/sms/email worker
global.smsWorker = require('child_process').fork('./smsWorker');
global.emailWorker = require('child_process').fork('./emailWorker');
global.notifiWorker = require('child_process').fork('./notifWorker');
@WaleedAshraf
WaleedAshraf / workers-mq.js
Last active July 5, 2018 02:57
Switching from cluster module to PM2 & RabbitMQ
// exports RabbitMQ connection
const MQ = require('./rabbitmq-config');
global.smsWorker = {
send: function (message) {
// publish message on sms exchange
return MQ.publish('sms', message);
}
};
@WaleedAshraf
WaleedAshraf / cluster-module.js
Last active July 5, 2018 02:57
Switching from cluster module to PM2 & RabbitMQ
var cluster = require('cluster');
const numCPUs = require('os').cpus().length;
module.exports.create = function (options, callback) {
if (cluster.isMaster) {
// fork child process for notif/sms/email worker
global.smsWorker = require('child_process').fork('./smsWorker');
global.emailWorker = require('child_process').fork('./emailWorker');
global.notifiWorker = require('child_process').fork('./notifWorker');
@WaleedAshraf
WaleedAshraf / nodejs-major-changes.md
Last active April 25, 2018 16:36
A list of Major/Notable changes in Node.js versions to help users migrate.

Version 5.0.0

  • buffer: (Breaking) Removed both 'raw' and 'raws' encoding types from Buffer, these have been deprecated for a long time (Sakthipriyan Vairamani) #2859.
  • console: (Breaking) Values reported by console.time() now have 3 decimals of accuracy added (Michaël Zasso) #3166.
  • fs:
    • fs.readFile*(), fs.writeFile*(), and fs.appendFile*() now also accept a file descriptor as their first argument (Johannes Wüller) #3163.
    • (Breaking) In fs.readFile(), if an encoding is specified and the internal toString() fails the error is no longer thrown but is passed to the callback (Evan Lucas) #3485.
  • (Breaking) In fs.read() (using the fs.read(fd, length, position, encoding, callback) form), if the internal toString() fails the error is no longer thrown but is passe
@WaleedAshraf
WaleedAshraf / serverless-app-1.js
Last active April 20, 2018 20:58
Building you first Serverless app in Node.js with AWS Lambda + S3 + API Gateway
exports.handler = async (event) => {
return sendRes(200,'Hello');
};
const sendRes = (status, body) => {
var response = {
statusCode: status,
headers: {
"Content-Type": "text/html"
},
@WaleedAshraf
WaleedAshraf / serverless-app-2.js
Last active April 20, 2018 20:58
Building you first Serverless app in Node.js with AWS Lambda + S3 + API Gateway
exports.handler = async (event) => {
const operation = event.queryStringParameters ? event.queryStringParameters.operation : null;
let data = JSON.parse(event.body);
switch (operation) {
case 'ping':
return sendRes(200, 'pong');
case 'convert':
return await operate(data);
default:
return sendRes(401, `Unrecognized operation "${operation}"`);