Skip to content

Commit

Permalink
setup runtime tests in order to test cookie behavior on browser and C…
Browse files Browse the repository at this point in the history
…ordova (which doesn't support cookies without workarounds)
  • Loading branch information
menelike committed Oct 13, 2019
0 parents commit e34ae23
Show file tree
Hide file tree
Showing 16 changed files with 942 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
.idea/
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "packages/Meteor-Cookies"]
path = packages/Meteor-Cookies
url = https://github.com/VeliovGroup/Meteor-Cookies.git
18 changes: 18 additions & 0 deletions .meteor/.finished-upgraders
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# This file contains information which helps Meteor properly upgrade your
# app when you run 'meteor update'. You should check it into version control
# with your project.

notices-for-0.9.0
notices-for-0.9.1
0.9.4-platform-file
notices-for-facebook-graph-api-2
1.2.0-standard-minifiers-package
1.2.0-meteor-platform-split
1.2.0-cordova-changes
1.2.0-breaking-changes
1.3.0-split-minifiers-package
1.4.0-remove-old-dev-bundle-link
1.4.1-add-shell-server-package
1.4.3-split-account-service-packages
1.5-add-dynamic-import-package
1.7-split-underscore-from-meteor-base
1 change: 1 addition & 0 deletions .meteor/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
local
7 changes: 7 additions & 0 deletions .meteor/.id
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This file contains a token that is unique to your project.
# Check it into your repository along with the rest of this directory.
# It can be used for purposes such as:
# - ensuring you don't accidentally deploy one app on top of another
# - providing package authors with aggregated statistics

g3ylf5ny3q3.dno8tvbrwc5
20 changes: 20 additions & 0 deletions .meteor/packages
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Meteor packages used by this project, one per line.
# Check this file (and the other files in this directory) into your repository.
#
# 'meteor add' and 'meteor remove' will edit this file for you,
# but you can also edit it by hand.

[email protected] # Shared foundation for all Meteor packages
static-html # Define static page content in .html files
[email protected] # CSS minifier run for production mode
[email protected] # JS minifier run for production mode
[email protected] # ECMAScript 5 compatibility for older browsers
[email protected] # Enable ECMAScript2015+ syntax in app code
[email protected] # Server-side component of the `meteor shell` command
[email protected] # Serves a Meteor app over HTTP
[email protected] # Support for server-side rendering
ostrio:cookies
http
meteor-base
browser-policy

4 changes: 4 additions & 0 deletions .meteor/platforms
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
android
browser
ios
server
1 change: 1 addition & 0 deletions .meteor/release
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[email protected]
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.meteor
packages
9 changes: 9 additions & 0 deletions client/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<head>
<title>ostrio:cookies test app</title>
</head>

<body>
<h1>ostrio:cookies test app</h1>
<ul id="output">
</ul>
</body>
189 changes: 189 additions & 0 deletions client/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
import { Cookies } from "meteor/ostrio:cookies";
import { HTTP } from "meteor/http";
import { Meteor } from "meteor/meteor";

const attachMsg = (node) => {
const list = document.getElementById("output");
list.appendChild(node);
};

const createListItem = (msg, style) => {
const node = document.createElement("li");
node.appendChild(document.createTextNode(msg));
node.setAttribute("style", style);
return node;
};

const assertEqual = (a, b, msg) => {
const node = createListItem(msg, `color: ${a === b ? "green" : "red"};`);
attachMsg(node);
};

const runTests = async () => {
const cookieValue = `${Date.now()}`;
const cookieParam = `__TEST_COOKIE__=${cookieValue}`;
const ___expectedCookies___ = encodeURI(cookieParam);
const cookieResetPath = Meteor.absoluteUrl("/__cookie_reset");
const cookieTestPath = Meteor.absoluteUrl("/__cookie_match");
const cookies = new Cookies({ allowQueryStringCookies: true });

cookies.remove();
assertEqual(document.cookie, "", "cookie is empty");
await new Promise(resolve => {
HTTP.get(
cookieResetPath,
{
params: {
___removeCookie___: "__TEST_COOKIE__"
},
beforeSend(xhr) {
xhr.withCredentials = true;
return true;
}
},
(err, res) => {
assertEqual(res.statusCode, 200, "clear cookie request successful");
resolve();
}
);
});

await new Promise(resolve => {
HTTP.get(
cookieTestPath,
{
params: {
___expectedCookies___: "undefined"
},
beforeSend(xhr) {
xhr.withCredentials = true;
return true;
}
},
(err, res) => {
assertEqual(res.statusCode, 200, "server did not receive cookie");
resolve();
}
);
});

cookies.set("__TEST_COOKIE__", cookieValue);
assertEqual(document.cookie, cookieParam, "set cookie on client");

if (Meteor.isCordova) {
await new Promise(resolve => {
HTTP.get(
cookieTestPath,
{
params: {
___expectedCookies___: "undefined"
},
beforeSend(xhr) {
xhr.withCredentials = true;
return true;
}
},
(err, res) => {
assertEqual(res.statusCode, 200, "no cookie received from cordova");
resolve();
}
);
});
} else {
await new Promise(resolve => {
HTTP.get(
cookieTestPath,
{
params: {
___expectedCookies___
}
},
(err, res) => {
assertEqual(
res.statusCode,
200,
"server received cookie from browser"
);
resolve();
}
);
});
}

await new Promise(resolve => cookies.send(resolve));

// in cordova cookies set by the server are invisible by the client
// but are sent within requests if withCredentials is true
await new Promise(resolve => {
HTTP.get(
cookieTestPath,
{
params: {
___expectedCookies___
},
beforeSend(xhr) {
xhr.withCredentials = true;
return true;
}
},
(err, res) => {
assertEqual(res.statusCode, 200, "server received cookie after send()");
resolve();
}
);
});

if (Meteor.isCordova) {
await new Promise(resolve => {
HTTP.get(
cookieTestPath,
{
params: {
___expectedCookies___: "undefined"
}
},
(err, res) => {
assertEqual(
res.statusCode,
200,
"no cookie from cordova with withCredentials = false"
);
resolve();
}
);
});
} else {
await new Promise(resolve => {
HTTP.get(
cookieTestPath,
{
params: {
___expectedCookies___
}
},
(err, res) => {
assertEqual(
res.statusCode,
200,
"server received cookie from browser with withCredentials = false"
);
resolve();
}
);
});
}

attachMsg(createListItem("all tests run!"));
};

console.log(document.readyState);

const func = () => {
if (document.readyState === "complete") {
runTests().catch(console.error);
} else {
setTimeout(func, 10)
}
};

func();
Loading

0 comments on commit e34ae23

Please sign in to comment.