0

So I'm building a simple web app with express and wanted to unit test from the start. So I created a simple test to see if '/' returns a 200. Yet my tests always say they receive a 426. I can't find anything online about this issue, and believe I am setting up the server correctly.

Here is my server:

// server.js    
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const winston = require('winston')

const app = express();

const logger = winston.createLogger({
    level: 'info',
    format: winston.format.json(),
    defaultMeta: { service: 'user-service' },
    transports: [
        new winston.transports.File({ filename: 'error.log', level: 'error' }),
        new winston.transports.File({ filename: 'combined.log' })
    ]
})

if (process.env.NODE_ENV !== 'production') {
    logger.add(new winston.transports.Console({
        format: winston.format.simple()
    }))
}

app.use(
  bodyParser.urlencoded({
    extended: false,
  })
);
app.use(bodyParser.json());
app.use(cors());

app.get('/', (req, res) => {
    logger.info("recieved get to /")
    res.send('ok')
});

const port = 3000;

const server = app.listen(port, err => {
  if (err) {
    console.log(err);
  } else {
    console.log(`Running on port ${port}`);
  }
});
module.exports = server

and here are my tests:

const request = require('supertest')

describe('loading express', () => {
    let server;
    process.env.NODE_ENV = 'test'
    beforeEach(() => {
        delete require.cache[require.resolve('../server')]; // clear the cache of server instance
        server = require('../server')
    })
    afterEach((done) => {
        server.close(done)
    })
    it('responds to /', (done) => {
        request(server)
            .get('/')
            .expect(200, done)
    })
    it('404 everything else', (done) => {
        request(server)
            .get('/foo/bar')
            .expect(404, done)
    })
})

and my tests always return:

0 passing (1s)
  2 failing

  1) loading express
       responds to /:
     Error: expected 200 "OK", got 426 "Upgrade Required"
      at Test._assertStatus (node_modules\supertest\lib\test.js:268:12)
      at Test._assertFunction (node_modules\supertest\lib\test.js:283:11)
      at Test.assert (node_modules\supertest\lib\test.js:173:18)
      at localAssert (node_modules\supertest\lib\test.js:131:12)
      at C:\Users\0024620\Desktop\dev-p\rank\node_modules\supertest\lib\test.js:128:5
      at Test.Request.callback (node_modules\superagent\lib\node\index.js:728:3)
      at IncomingMessage.<anonymous> (node_modules\superagent\lib\node\index.js:916:18)
      at endReadableNT (_stream_readable.js:1137:12)
      at processTicksAndRejections (internal/process/task_queues.js:84:9)

Dependencies from package.json

{
  "devDependencies": {
    "mocha": "^7.0.0",
    "supertest": "^4.0.2"
  },
  "dependencies": {
    "express": "^4.17.1",
    "@pusher/chatkit-server": "^1.0.4",
    "body-parser": "^1.18.2",
    "express-handlebars": "^3.0.0",
    "cors": "^2.8.5",
    "passport": "^0.4.1",
    "winston": "^3.2.0"
  }
}
Mickael B.
  • 4,755
  • 4
  • 24
  • 48
timafield
  • 41
  • 5
  • I tried myself and it worked fine I just installed the required packages `npm i express winston body-parser cors supertest mocha`. Maybe you don't have the latest version can you check the versions of node and your dependencies ? – Mickael B. Jan 10 '20 at 21:09
  • added package.json, all my packages should be the latest versions. 426 Update Required doesn't have anything to do with node package versions though. https://tools.ietf.org/html/rfc7231#section-6.5.15: "...indicates that the server refuses to perform the request using the current protocol but might be willing to do so after the client upgrades to a different protocol." – timafield Jan 10 '20 at 21:29
  • Note, it is only during unit testing that I receive the 426. When I open the page in a browser I get the expected 200 ok – timafield Jan 10 '20 at 21:33
  • Can you add `app.use((req, res, next) => { console.log(req.httpVersion); next(); });` as first middleware to see what HTTP version it's using – Mickael B. Jan 10 '20 at 21:43
  • Nothing is printing from inside use's function. Neither when I'm in the testing suite or starting the server normally. It just says "Running on port 3000." Console logging in other parts of the server code printed just fine. – timafield Jan 10 '20 at 22:00
  • Not even an empty line between your info log ? Try to log the `req` and search for some `httpVersion` attribute – Mickael B. Jan 10 '20 at 22:05
  • Ah, okay the http version is 1.1 – timafield Jan 10 '20 at 22:14
  • I don't know then, it should work, have you tried this part of code alone in another folder and reinstalling dependencies. – Mickael B. Jan 10 '20 at 22:21
  • No dice. Moved to new directory, reinstalled everything. Same result. I'll try it on my home computer to see if it is something funny with my work computer. – timafield Jan 13 '20 at 15:59
  • It works fine on my home computer. Must be something to do with my work computer's security. – timafield Jan 14 '20 at 15:16

1 Answers1

1

Hi the answer can be found here Getting over a 426 upgrade required . It seems a lot of work PCs have a problem with port 3000.