|
| 1 | +var lib = require("../"); |
| 2 | +var helper = require("./helper"); |
| 3 | +var nock = require("nock"); |
| 4 | +var Geocode = lib.Geocode; |
| 5 | + |
| 6 | +describe("Geocode", function(){ |
| 7 | + before(function(){ |
| 8 | + nock.disableNetConnect(); |
| 9 | + helper.setupGlobalOptions(); |
| 10 | + }); |
| 11 | + after(function(){ |
| 12 | + nock.cleanAll(); |
| 13 | + nock.enableNetConnect(); |
| 14 | + }); |
| 15 | + describe("#request", function(){ |
| 16 | + it("should make a geocode request", function(done){ |
| 17 | + geoData = { |
| 18 | + addressLine1: "1 Street Name", |
| 19 | + city: "City", |
| 20 | + stateCode: "State", |
| 21 | + zip: "ZipCode" |
| 22 | + } |
| 23 | + helper.nock().post("/accounts/FakeAccountId/geocodeRequest", helper.buildXml({requestAddress: geoData})).reply(200, helper.xml.geocode, {"Content-Type": "application/xml"}); |
| 24 | + Geocode.request(helper.createClient(), geoData, function(err, geocode){ |
| 25 | + if(err){ |
| 26 | + return done(err); |
| 27 | + } |
| 28 | + geocode.houseNumber.should.eql(1); |
| 29 | + geocode.streetName.should.eql("Street"); |
| 30 | + geocode.streetSuffix.should.eql("Name"); |
| 31 | + geocode.city.should.eql("City"); |
| 32 | + geocode.stateCode.should.eql("State"); |
| 33 | + geocode.zip.should.eql("ZipCode"); |
| 34 | + geocode.plusFour.should.eql(1234); |
| 35 | + geocode.country.should.eql("US"); |
| 36 | + done(); |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + it("should make a geocode with default client", function(done){ |
| 41 | + geoData = { |
| 42 | + addressLine1: "1 Street Name", |
| 43 | + city: "City", |
| 44 | + stateCode: "State", |
| 45 | + zip: "ZipCode" |
| 46 | + } |
| 47 | + helper.nock().post("/accounts/FakeAccountId/geocodeRequest", helper.buildXml({requestAddress: geoData})).reply(200, helper.xml.geocode, {"Content-Type": "application/xml"}); |
| 48 | + Geocode.request(geoData, function(err, geocode){ |
| 49 | + if(err){ |
| 50 | + return done(err); |
| 51 | + } |
| 52 | + geocode.houseNumber.should.eql(1); |
| 53 | + geocode.streetName.should.eql("Street"); |
| 54 | + geocode.streetSuffix.should.eql("Name"); |
| 55 | + geocode.city.should.eql("City"); |
| 56 | + geocode.stateCode.should.eql("State"); |
| 57 | + geocode.zip.should.eql("ZipCode"); |
| 58 | + geocode.plusFour.should.eql(1234); |
| 59 | + geocode.country.should.eql("US"); |
| 60 | + done(); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + it("should handle 409 collision without error", function(done){ |
| 65 | + geoData = { |
| 66 | + addressLine1: "123 Street Name", |
| 67 | + city: "City", |
| 68 | + stateCode: "State", |
| 69 | + zip: "ZipCode" |
| 70 | + } |
| 71 | + helper.nock().post("/accounts/FakeAccountId/geocodeRequest", helper.buildXml({requestAddress: geoData})).reply(409, helper.xml.geocode, {"Content-Type": "application/xml"}); |
| 72 | + Geocode.request(helper.createClient(), geoData, function(err, geocode){ |
| 73 | + if(err){ |
| 74 | + return done(err); |
| 75 | + } |
| 76 | + geocode.houseNumber.should.eql(1); |
| 77 | + geocode.streetName.should.eql("Street"); |
| 78 | + geocode.streetSuffix.should.eql("Name"); |
| 79 | + geocode.city.should.eql("City"); |
| 80 | + geocode.stateCode.should.eql("State"); |
| 81 | + geocode.zip.should.eql("ZipCode"); |
| 82 | + geocode.plusFour.should.eql(1234); |
| 83 | + geocode.country.should.eql("US"); |
| 84 | + done(); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + it("should report errors", function(done){ |
| 89 | + geoData = { |
| 90 | + addressLine1: "Bad adrress line 1", |
| 91 | + city: "City", |
| 92 | + stateCode: "State", |
| 93 | + zip: "ZipCode" |
| 94 | + } |
| 95 | + helper.nock().post("/accounts/FakeAccountId/geocodeRequest", helper.buildXml({requestAddress: geoData})).reply(400, {"Content-Type": "application/xml"}); |
| 96 | + Geocode.request(helper.createClient(), geoData, function(err, geocode){ |
| 97 | + if(err){ |
| 98 | + return done(); |
| 99 | + } |
| 100 | + done(new Error('An error is estimated')); |
| 101 | + }); |
| 102 | + }); |
| 103 | + }); |
| 104 | +}); |
0 commit comments