Skip to content

Commit

Permalink
uses axios in basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sintaxi committed Aug 13, 2022
1 parent e0ccfe4 commit aa534f4
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 52 deletions.
74 changes: 74 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"terraform": "1.21.2"
},
"devDependencies": {
"axios": "0.27.2",
"mocha": "10.0.0",
"request": "^2.88.0",
"should": "3.3.2"
Expand Down
28 changes: 14 additions & 14 deletions test/auth.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var should = require("should")
var request = require('request')
var axios = require('axios')
var path = require('path')
var harp = require("../")

Expand All @@ -8,29 +8,29 @@ describe("basicAuth", function(){
describe("single", function(done){
var projectPath = path.join(__dirname, "apps/auth/single")
var server;

before(function(done){
server = harp.server(projectPath).listen(8310, done)
})

it("should be a protected page", function(done){
request('http://localhost:8310/', function (e, r, b) {
r.statusCode.should.eql(401)
axios.get('http://localhost:8310/').catch(function(error) {
error.response.status.should.eql(401)
done()
})
})

it("should fetch protected resource with correct creds", function(done){
request({
method: "GET",
url: 'http://localhost:8310/',
auth: {
'user': 'foo',
'pass': 'bar'
}
}, function (e, r, b) {
r.statusCode.should.eql(200)
done()
axios({
method: "GET",
url: 'http://localhost:8310/',
auth: {
username: 'foo',
password: 'bar'
}
}).then(function(response){
response.status.should.eql(200)
done()
})
})

Expand Down
74 changes: 36 additions & 38 deletions test/basic.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var should = require("should")
var request = require('request')
var axios = require('axios')
var path = require("path")
var fs = require("fs")
var exec = require("child_process").exec
Expand Down Expand Up @@ -27,11 +27,10 @@ describe("basic", function(){
var staticGlobals = require(path.join(outputPath, "globals.json"))
staticGlobals.should.have.property("environment", "production")
staticGlobals.should.have.property("public")
request('http://localhost:8100/globals.json', function (e, r, b) {
r.statusCode.should.eql(200)
var dynamicGlobals = JSON.parse(b)
dynamicGlobals.should.have.property("environment", "development")
dynamicGlobals.should.have.property("public")
axios.get('http://localhost:8100/globals.json').then(function(r){
r.status.should.eql(200)
r.data.should.have.property("environment", "development")
r.data.should.have.property("public")
done()
})
})
Expand All @@ -40,11 +39,10 @@ describe("basic", function(){
var staticCurrent = require(path.join(outputPath, "current.json"))
staticCurrent.should.have.property("path")
staticCurrent.should.have.property("source", "current.json")
request('http://localhost:8100/current.json', function (e, r, b) {
r.statusCode.should.eql(200)
var dynamicCurrent = JSON.parse(b)
dynamicCurrent.should.have.property("path")
dynamicCurrent.should.have.property("source", "current.json")
axios.get('http://localhost:8100/current.json').then(function(r){
r.status.should.eql(200)
r.data.should.have.property("path")
r.data.should.have.property("source", "current.json")
done()
})
})
Expand All @@ -53,10 +51,10 @@ describe("basic", function(){
fs.readFile(path.join(outputPath, "index.html"), function(err, contents){
contents.toString().should.include("Kitchen Sink")
contents.toString().should.include("Home")
request('http://localhost:8100/', function (e, r, b) {
r.statusCode.should.eql(200)
b.should.include("Kitchen Sink")
b.should.include("Home")
axios.get('http://localhost:8100/').then(function(r){
r.status.should.eql(200)
r.data.should.include("Kitchen Sink")
r.data.should.include("Home")
done()
})
})
Expand All @@ -66,12 +64,12 @@ describe("basic", function(){
fs.readFile(path.join(outputPath, "404.html"), function(err, contents){
contents.toString().should.not.include("Kitchen Sink")
contents.toString().should.include("Custom, Page Not Found")
request('http://localhost:8100/some/missing/path', function (e, r, b) {
r.statusCode.should.eql(404)
r.headers.should.have.property("content-type", "text/html; charset=UTF-8")
b.should.not.include("Kitchen Sink")
b.should.include("Custom, Page Not Found")
b.should.eql(contents.toString())
axios.get('http://localhost:8100/some/missing/path').catch(function(e){
e.response.status.should.eql(404)
e.response.headers.should.have.property("content-type", "text/html; charset=UTF-8")
e.response.data.should.not.include("Kitchen Sink")
e.response.data.should.include("Custom, Page Not Found")
e.response.data.should.eql(contents.toString())
done()
})
})
Expand All @@ -80,19 +78,19 @@ describe("basic", function(){
it("should return CSS file", function(done){
fs.readFile(path.join(outputPath, "css", "main.css"), function(err, contents){
contents.toString().should.include("background")
request('http://localhost:8100/css/main.css', function (e, r, b) {
r.statusCode.should.eql(200)
b.should.include("background")
b.should.eql(contents.toString())
axios.get('http://localhost:8100/css/main.css').then(function(r){
r.status.should.eql(200)
r.data.should.include("background")
r.data.should.eql(contents.toString())
done()
})
})
})

it("should return proper mime type on 404 page", function(done){
request('http://localhost:8100/some/missing/path.css', function (e, r, b) {
r.statusCode.should.eql(404)
r.headers.should.have.property("content-type", "text/html; charset=UTF-8")
axios.get('http://localhost:8100/some/missing/path.css').catch(function(e){
e.response.status.should.eql(404)
e.response.headers.should.have.property("content-type", "text/html; charset=UTF-8")
done()
})
})
Expand All @@ -101,27 +99,27 @@ describe("basic", function(){
fs.readFile(path.join(outputPath, "basic.html"), function(err, contents){
contents.toString().should.not.include("Kitchen Sink")
contents.toString().should.include("<h1>Basic HTML Page</h1>")
request('http://localhost:8100/basic', function(e,r,b){
r.statusCode.should.eql(200)
b.should.not.include("Kitchen Sink")
b.should.include("<h1>Basic HTML Page</h1>")
b.should.eql(contents.toString())
axios.get('http://localhost:8100/basic').then(function(r){
r.status.should.eql(200)
r.data.should.not.include("Kitchen Sink")
r.data.should.include("<h1>Basic HTML Page</h1>")
r.data.should.eql(contents.toString())
done()
})
})
})

it("should not return file starting with underscore", function(done){
request('http://localhost:8100/shared/_nav.jade', function(e,r,b){
r.statusCode.should.eql(404)
axios.get('http://localhost:8100/shared/_nav.jade').catch(function(e){
e.response.status.should.eql(404)
done()
})
})

it("should render HTML page with spaces in the file name", function(done){
request('http://localhost:8100/articles/with%20spaces', function(e,r,b){
r.statusCode.should.eql(200)
b.should.include("foo article")
axios.get('http://localhost:8100/articles/with%20spaces').then(function(r){
r.status.should.eql(200)
r.data.should.include("foo article")
done()
})
})
Expand Down

0 comments on commit aa534f4

Please sign in to comment.