2014-08-01 02:51:10 +08:00
|
|
|
function parsePostData(query) {
|
2015-03-14 00:08:28 +08:00
|
|
|
const result = {};
|
2014-08-01 02:51:10 +08:00
|
|
|
query.split("&").forEach(function(part) {
|
2015-03-14 00:08:28 +08:00
|
|
|
const item = part.split("=");
|
2015-04-10 02:54:17 +08:00
|
|
|
const firstSeg = decodeURIComponent(item[0]);
|
|
|
|
const m = /^([^\[]+)\[([^\]]+)\]/.exec(firstSeg);
|
|
|
|
|
|
|
|
const val = decodeURIComponent(item[1]).replace(/\+/g, ' ');
|
|
|
|
if (m) {
|
|
|
|
result[m[1]] = result[m[1]] || {};
|
|
|
|
result[m[1]][m[2]] = val;
|
|
|
|
} else {
|
|
|
|
result[firstSeg] = val;
|
|
|
|
}
|
|
|
|
|
2014-08-01 02:51:10 +08:00
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-08-01 05:26:44 +08:00
|
|
|
function response(code, obj) {
|
2014-08-01 02:51:10 +08:00
|
|
|
if (typeof code === "object") {
|
|
|
|
obj = code;
|
|
|
|
code = 200;
|
|
|
|
}
|
2014-08-01 05:26:44 +08:00
|
|
|
return [code, {"Content-Type": "application/json"}, obj];
|
2014-08-01 02:51:10 +08:00
|
|
|
}
|
|
|
|
|
2014-10-08 04:03:51 +08:00
|
|
|
function success() {
|
2014-12-13 02:13:18 +08:00
|
|
|
return response({ success: true });
|
2014-10-08 04:03:51 +08:00
|
|
|
}
|
|
|
|
|
2015-03-07 01:37:24 +08:00
|
|
|
const _widgets = [
|
|
|
|
{id: 123, name: 'Trout Lure'},
|
|
|
|
{id: 124, name: 'Evil Repellant'}
|
|
|
|
];
|
|
|
|
|
2015-03-17 03:14:33 +08:00
|
|
|
const _moreWidgets = [
|
|
|
|
{id: 223, name: 'Bass Lure'},
|
|
|
|
{id: 224, name: 'Good Repellant'}
|
|
|
|
];
|
|
|
|
|
2015-04-02 02:18:46 +08:00
|
|
|
function loggedIn() {
|
|
|
|
return !!Discourse.User.current();
|
|
|
|
}
|
|
|
|
|
2014-08-01 02:51:10 +08:00
|
|
|
export default function() {
|
2015-04-02 02:18:46 +08:00
|
|
|
|
2015-03-14 00:08:28 +08:00
|
|
|
const server = new Pretender(function() {
|
2014-08-02 05:26:43 +08:00
|
|
|
|
2015-04-02 02:18:46 +08:00
|
|
|
const fixturesByUrl = {};
|
|
|
|
|
2014-08-02 05:26:43 +08:00
|
|
|
// Load any fixtures automatically
|
2015-03-14 00:08:28 +08:00
|
|
|
const self = this;
|
2014-08-02 05:26:43 +08:00
|
|
|
Ember.keys(require._eak_seen).forEach(function(entry) {
|
|
|
|
if (/^fixtures/.test(entry)) {
|
2015-03-14 00:08:28 +08:00
|
|
|
const fixture = require(entry, null, null, true);
|
2014-08-02 05:26:43 +08:00
|
|
|
if (fixture && fixture.default) {
|
2015-03-14 00:08:28 +08:00
|
|
|
const obj = fixture.default;
|
2014-08-02 05:26:43 +08:00
|
|
|
Ember.keys(obj).forEach(function(url) {
|
2015-04-02 02:18:46 +08:00
|
|
|
fixturesByUrl[url] = obj[url];
|
2014-08-02 05:26:43 +08:00
|
|
|
self.get(url, function() {
|
|
|
|
return response(obj[url]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-04-02 02:18:46 +08:00
|
|
|
this.get('/composer-messages', () => { return response([]); });
|
|
|
|
|
|
|
|
this.get("/latest.json", () => {
|
|
|
|
const json = fixturesByUrl['/latest.json'];
|
|
|
|
|
|
|
|
if (loggedIn()) {
|
|
|
|
// Stuff to let us post
|
|
|
|
json.topic_list.can_create_topic = true;
|
|
|
|
json.topic_list.draft_key = "new_topic";
|
|
|
|
json.topic_list.draft_sequence = 1;
|
|
|
|
}
|
|
|
|
return response(json);
|
|
|
|
});
|
|
|
|
|
2014-09-17 23:18:41 +08:00
|
|
|
this.get("/t/id_for/:slug", function() {
|
|
|
|
return response({id: 280, slug: "internationalization-localization", url: "/t/internationalization-localization/280"});
|
|
|
|
});
|
|
|
|
|
2014-08-02 05:26:43 +08:00
|
|
|
this.get("/404-body", function() {
|
|
|
|
return [200, {"Content-Type": "text/html"}, "<div class='page-not-found'>not found</div>"];
|
|
|
|
});
|
|
|
|
|
2015-04-10 06:33:37 +08:00
|
|
|
this.delete('/draft.json', success);
|
|
|
|
|
2014-08-02 05:26:43 +08:00
|
|
|
this.get('/draft.json', function() {
|
|
|
|
return response({});
|
|
|
|
});
|
2015-03-13 14:45:55 +08:00
|
|
|
|
2014-08-01 02:51:10 +08:00
|
|
|
this.post('/session', function(request) {
|
2015-03-14 00:08:28 +08:00
|
|
|
const data = parsePostData(request.requestBody);
|
2014-08-01 02:51:10 +08:00
|
|
|
|
|
|
|
if (data.password === 'correct') {
|
2014-08-01 05:26:44 +08:00
|
|
|
return response({username: 'eviltrout'});
|
2014-08-01 02:51:10 +08:00
|
|
|
}
|
2014-08-01 05:26:44 +08:00
|
|
|
return response(400, {error: 'invalid login'});
|
2014-08-01 02:51:10 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
this.get('/users/hp.json', function() {
|
2014-08-01 05:26:44 +08:00
|
|
|
return response({"value":"32faff1b1ef1ac3","challenge":"61a3de0ccf086fb9604b76e884d75801"});
|
2014-08-01 02:51:10 +08:00
|
|
|
});
|
|
|
|
|
2014-08-01 05:06:00 +08:00
|
|
|
this.get('/session/csrf', function() {
|
2014-08-01 05:26:44 +08:00
|
|
|
return response({"csrf":"mgk906YLagHo2gOgM1ddYjAN4hQolBdJCqlY6jYzAYs="});
|
2014-08-01 05:06:00 +08:00
|
|
|
});
|
|
|
|
|
2014-08-01 02:51:10 +08:00
|
|
|
this.get('/users/check_username', function(request) {
|
|
|
|
if (request.queryParams.username === 'taken') {
|
2014-08-01 05:26:44 +08:00
|
|
|
return response({available: false, suggestion: 'nottaken'});
|
2014-08-01 02:51:10 +08:00
|
|
|
}
|
2014-08-01 05:26:44 +08:00
|
|
|
return response({available: true});
|
2014-08-01 02:51:10 +08:00
|
|
|
});
|
|
|
|
|
2014-08-01 05:26:44 +08:00
|
|
|
this.post('/users', function() {
|
|
|
|
return response({success: true});
|
2014-08-01 02:51:10 +08:00
|
|
|
});
|
2014-08-01 05:59:52 +08:00
|
|
|
|
|
|
|
this.get('/login.html', function() {
|
|
|
|
return [200, {}, 'LOGIN PAGE'];
|
|
|
|
});
|
2014-10-08 04:03:51 +08:00
|
|
|
|
|
|
|
this.delete('/posts/:post_id', success);
|
|
|
|
this.put('/posts/:post_id/recover', success);
|
2015-03-07 01:37:24 +08:00
|
|
|
|
2015-04-02 02:18:46 +08:00
|
|
|
this.put('/posts/:post_id', (request) => {
|
2015-04-10 02:54:17 +08:00
|
|
|
const data = parsePostData(request.requestBody);
|
|
|
|
data.post.id = request.params.post_id;
|
|
|
|
data.post.version = 2;
|
|
|
|
return response(200, data.post);
|
2015-04-02 02:18:46 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
this.put('/t/:slug/:id', (request) => {
|
|
|
|
const data = parsePostData(request.requestBody);
|
|
|
|
|
|
|
|
return response(200, { basic_topic: {id: request.params.id,
|
|
|
|
title: data.title,
|
|
|
|
fancy_title: data.title,
|
2015-04-09 02:17:21 +08:00
|
|
|
slug: request.params.slug } });
|
2015-04-02 02:18:46 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
this.post('/posts', function(request) {
|
|
|
|
const data = parsePostData(request.requestBody);
|
|
|
|
|
|
|
|
if (data.title === "this title triggers an error") {
|
|
|
|
return response(422, {errors: ['That title has already been taken']});
|
|
|
|
}
|
2015-04-10 06:33:37 +08:00
|
|
|
|
|
|
|
if (data.raw === "enqueue this content please") {
|
|
|
|
return response(200, { success: true, action: 'enqueued' });
|
|
|
|
}
|
|
|
|
|
|
|
|
return response(200, {
|
|
|
|
success: true,
|
|
|
|
action: 'create_post',
|
|
|
|
post: {id: 12345, topic_id: 280, topic_slug: 'internationalization-localization'}
|
|
|
|
});
|
2015-04-02 02:18:46 +08:00
|
|
|
});
|
|
|
|
|
2015-03-07 01:37:24 +08:00
|
|
|
this.get('/widgets/:widget_id', function(request) {
|
|
|
|
const w = _widgets.findBy('id', parseInt(request.params.widget_id));
|
|
|
|
if (w) {
|
|
|
|
return response({widget: w});
|
|
|
|
} else {
|
|
|
|
return response(404);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-04-10 02:54:17 +08:00
|
|
|
this.post('/widgets', function(request) {
|
|
|
|
const widget = parsePostData(request.requestBody).widget;
|
|
|
|
widget.id = 100;
|
|
|
|
return response(200, {widget});
|
|
|
|
});
|
|
|
|
|
2015-03-07 01:56:32 +08:00
|
|
|
this.put('/widgets/:widget_id', function(request) {
|
2015-04-10 02:54:17 +08:00
|
|
|
const widget = parsePostData(request.requestBody).widget;
|
|
|
|
return response({ widget });
|
2015-03-17 03:14:33 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
this.get('/widgets', function(request) {
|
|
|
|
let result = _widgets;
|
|
|
|
|
|
|
|
const qp = request.queryParams;
|
|
|
|
if (qp) {
|
|
|
|
if (qp.name) { result = result.filterBy('name', qp.name); }
|
|
|
|
if (qp.id) { result = result.filterBy('id', parseInt(qp.id)); }
|
|
|
|
}
|
|
|
|
|
|
|
|
return response({ widgets: result, total_rows_widgets: 4, load_more_widgets: '/load-more-widgets' });
|
2015-03-07 01:56:32 +08:00
|
|
|
});
|
|
|
|
|
2015-03-17 03:14:33 +08:00
|
|
|
this.get('/load-more-widgets', function() {
|
|
|
|
return response({ widgets: _moreWidgets, total_rows_widgets: 4, load_more_widgets: '/load-more-widgets' });
|
2015-03-07 01:37:24 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
this.delete('/widgets/:widget_id', success);
|
2014-08-01 02:51:10 +08:00
|
|
|
|
2015-04-02 02:18:46 +08:00
|
|
|
this.post('/topics/timings', function() {
|
|
|
|
return response(200, {});
|
|
|
|
});
|
|
|
|
});
|
2014-08-01 05:26:44 +08:00
|
|
|
|
|
|
|
server.prepareBody = function(body){
|
|
|
|
if (body && typeof body === "object") {
|
|
|
|
return JSON.stringify(body);
|
|
|
|
}
|
2014-08-02 05:26:43 +08:00
|
|
|
return body;
|
2014-08-01 05:26:44 +08:00
|
|
|
};
|
|
|
|
|
2014-08-01 02:51:10 +08:00
|
|
|
server.unhandledRequest = function(verb, path) {
|
2015-03-14 00:08:28 +08:00
|
|
|
const error = 'Unhandled request in test environment: ' + path + ' (' + verb + ')';
|
2014-08-01 06:44:32 +08:00
|
|
|
window.console.error(error);
|
|
|
|
throw error;
|
2014-08-01 02:51:10 +08:00
|
|
|
};
|
|
|
|
|
2015-03-14 00:08:28 +08:00
|
|
|
server.checkPassthrough = function(request) {
|
|
|
|
return request.requestHeaders['Discourse-Script'];
|
|
|
|
};
|
|
|
|
|
2014-08-01 02:51:10 +08:00
|
|
|
return server;
|
|
|
|
}
|