Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

Commit

Permalink
feat: let opensearch host can be config (#1258)
Browse files Browse the repository at this point in the history
fengmk2 authored Nov 22, 2017

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 820ae23 commit 56c9457
Showing 4 changed files with 20 additions and 17 deletions.
4 changes: 4 additions & 0 deletions config/index.js
Original file line number Diff line number Diff line change
@@ -229,6 +229,10 @@ var config = {
// global hook function: function* (envelope) {}
// envelope format please see https://github.com/npm/registry/blob/master/docs/hooks/hooks-payload.md#payload
globalHook: null,

opensearch: {
host: '',
},
};

if (process.env.NODE_ENV === 'test') {
4 changes: 3 additions & 1 deletion middleware/opensearch.js
Original file line number Diff line number Diff line change
@@ -8,11 +8,13 @@ var template = '<?xml version="1.0" encoding="UTF-8"?>\
<Url method="get" type="text/html" template="http://${host}/browse/keyword/{searchTerms}"/>\
</OpenSearchDescription>';

var config = require('../config');

module.exports = function* opensearch(next) {
if (this.path === '/opensearch.xml') {
this.type = 'text/xml';
this.charset = 'utf-8';
this.body = template.replace('${host}', this.host);
this.body = template.replace('${host}', config.opensearch.host || this.host);
}
yield next;
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -74,7 +74,7 @@
"istanbul": "*",
"jshint": "*",
"mm": "*",
"mocha": "*",
"mocha": "3",
"node-dev": "*",
"pedding": "*",
"pg": "5",
27 changes: 12 additions & 15 deletions test/middleware/opensearch.test.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
/**!
* cnpmjs.org - test/middleware/opensearch.test.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* dead_horse <[email protected]> (http://deadhorse.me)
*/

'use strict';

/**
* Module dependencies.
*/

var request = require('supertest');
var mm = require('mm');
var app = require('../../servers/web');
var config = require('../../config');

describe('middleware/opensearch.test.js', function () {
before(function (done) {
@@ -24,13 +12,22 @@ describe('middleware/opensearch.test.js', function () {
after(function (done) {
app.close(done);
});
afterEach(mm.restore);

describe('GET /opensearch.xml', function () {
it('should get 200', function (done) {
request(app)
.get('/opensearch.xml')
.set('host', 'localhost')
.expect(/http:\/\/localhost/, done);
.expect(/http:\/\/localhost\//, done);
});

it('should return custom opensearch host', function (done) {
mm(config.opensearch, 'host', 'foo.com');
request(app)
.get('/opensearch.xml')
.set('host', 'localhost:6002')
.expect(/http:\/\/foo\.com\/browse\/keyword\//, done);
});
});
});

0 comments on commit 56c9457

Please sign in to comment.