Skip to content

Commit

Permalink
Merge a81ab03 into 6db6435
Browse files Browse the repository at this point in the history
  • Loading branch information
bartekn committed Oct 25, 2018
2 parents 6db6435 + a81ab03 commit c1a3cd0
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/call_builder.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {NotFoundError, NetworkError, BadRequestError} from "./errors";
import HorizonAxiosClient from './horizon_axios_client';
import forEach from 'lodash/forEach';

let URI = require("urijs");
let URITemplate = require("urijs/src/URITemplate");

let axios = require("axios");
var EventSource = (typeof window === 'undefined') ? require('eventsource') : window.EventSource;

/**
Expand Down Expand Up @@ -162,7 +162,7 @@ export class CallBuilder {

// Temp fix for: https://github.com/stellar/js-stellar-sdk/issues/15
url.setQuery('c', Math.random());
return axios.get(url.toString())
return HorizonAxiosClient.get(url.toString())
.then(response => response.data)
.catch(this._handleNetworkError);
}
Expand Down
9 changes: 9 additions & 0 deletions src/horizon_axios_client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let axios = require("axios");
let version = require('../package.json').version;

export default axios.create({
headers: {
'X-Client-Name': 'js-stellar-sdk',
'X-Client-Version': version,
}
});
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
require('es6-promise').polyfill();
import HorizonAxiosClient from './horizon_axios_client';

// stellar-sdk classes to expose
export * from "./errors";
export {Config} from "./config";
export {Server} from "./server";
export {HorizonAxiosClient};
export {FederationServer, FEDERATION_RESPONSE_MAX_SIZE} from "./federation_server";
export {StellarTomlResolver, STELLAR_TOML_MAX_SIZE} from "./stellar_toml_resolver";

Expand Down
4 changes: 2 additions & 2 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import {EffectCallBuilder} from "./effect_call_builder";
import {FriendbotBuilder} from "./friendbot_builder";
import {AssetsCallBuilder} from "./assets_call_builder";
import { TradeAggregationCallBuilder } from "./trade_aggregation_call_builder";
import HorizonAxiosClient from './horizon_axios_client';
import {xdr} from "stellar-base";
import isString from "lodash/isString";

let axios = require("axios");
let URI = require("urijs");
let URITemplate = require("urijs").URITemplate;

Expand Down Expand Up @@ -54,7 +54,7 @@ export class Server {
*/
submitTransaction(transaction) {
let tx = encodeURIComponent(transaction.toEnvelope().toXDR().toString("base64"));
return axios.post(
return HorizonAxiosClient.post(
URI(this.serverURL).segment('transactions').toString(),
`tx=${tx}`,
{timeout: SUBMIT_TRANSACTION_TIMEOUT}
Expand Down
30 changes: 30 additions & 0 deletions test/integration/client_headers_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const http = require('http')
const port = 3000;

describe('integration tests', function() {
it("sends client headers", function(done) {
if (typeof window !== "undefined") {
done();
return;
}

var server;

const requestHandler = (request, response) => {
expect(request.headers['x-client-name']).to.be.equal('js-stellar-sdk');
expect(request.headers['x-client-version']).to.match(/^[0-9]+\.[0-9]+\.[0-9]+$/);
response.end();
server.close(() => done());
}

server = http.createServer(requestHandler);
server.listen(port, (err) => {
if (err) {
done(err);
return
}

new StellarSdk.Server(`http://localhost:${port}`, {allowHttp: true}).operations().call();
})
});
});
2 changes: 2 additions & 0 deletions test/test-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ if (typeof window === 'undefined') {
require("babel-register");
global.StellarSdk = require('../src/index');
global.axios = require("axios");
global.HorizonAxiosClient = StellarSdk.HorizonAxiosClient;
var chaiAsPromised = require("chai-as-promised");
global.chai = require('chai');
global.chai.should();
Expand All @@ -11,4 +12,5 @@ if (typeof window === 'undefined') {
global.expect = global.chai.expect;
} else {
window.axios = StellarSdk.axios;
window.HorizonAxiosClient = StellarSdk.HorizonAxiosClient;
}
2 changes: 1 addition & 1 deletion test/unit/horizon_path_test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
describe("horizon path tests", function () {

beforeEach(function () {
this.axiosMock = sinon.mock(axios);
this.axiosMock = sinon.mock(HorizonAxiosClient);
StellarSdk.Config.setDefault();
StellarSdk.Network.useTestNetwork();
});
Expand Down
2 changes: 1 addition & 1 deletion test/unit/server_test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
describe("server.js tests", function () {
beforeEach(function () {
this.server = new StellarSdk.Server('https://horizon-live.stellar.org:1337');
this.axiosMock = sinon.mock(axios);
this.axiosMock = sinon.mock(HorizonAxiosClient);
StellarSdk.Config.setDefault();
StellarSdk.Network.useTestNetwork();
});
Expand Down

0 comments on commit c1a3cd0

Please sign in to comment.