Ejemplo n.º 1
0
    def test_route(self):
        app = App()

        result = app.route('/')

        self.assertIsNotNone(result)
        self.assertIs(type(result), Router)
Ejemplo n.º 2
0
    def test_put(self):
        app = App()

        app.put('/', endware_1)
        result = app.put('/abc', endware_2)

        # make sure only the delete method is in there
        self.assertIsNone(app._route_path('/', 'GET')[1])
        self.assertIsNotNone(app._route_path('/', 'PUT')[1])
        self.assertIsNone(app._route_path('/', 'POST')[1])
        self.assertIsNone(app._route_path('/', 'DELETE')[1])

        # make sure the methods is correct one
        self.assertIs(app._route_path('/', 'PUT')[1][2], endware_1)
Ejemplo n.º 3
0
    def test_init(self):
        app = App()

        self.assertIsNotNone(app, msg='App non properly instantiated')
        self.assertIs(type(app.locals),
                      dict,
                      msg='App.locals should be {} on instantiation')
        self.assertIs(app.mountpath,
                      '',
                      msg='App.mountpath should be \'\' on instantiation')
Ejemplo n.º 4
0
    def listening():
        print("App is listening at localhost on port 8080")

    def log_incoming_request(req, res, next):
        print('[%s] %s' % (req.method, req.originalUrl) ) # print the request type and path
        next() # make sure the endware is reached

    def serve_homepage(req, res):
        homepage = open(os.path.join(os.path.dirname(__file__), 'home.html'), 'r').read() # read the file as a string
        homepage = homepage.replace('{{names}}', names_to_html()) # replace {{names}} with a list

        res.status(200).set('Content-Type', 'text/html').send( homepage ) # send it back as html

    def post_name(req, res):
        names.append(req.body['name']) # add the name to the list
        serve_homepage(req, res) # serve the homepage the same

    app = App()
    router = Router()

    # mount the endware
    router.get('/', serve_homepage)
    router.post('/', post_name)

    # mount the middleware
    app.use(router)
    app.use(log_incoming_request)

    # start the server
    app.listen(port=8080, callback=listening)
Ejemplo n.º 5
0
    def test_all(self):
        app = App()

        app.all('/', endware_1)
        result = app.all('/abc', endware_2)

        self.assertIs(result, app)  # make sure it returns the App again

        # make sure all the methods are in there
        self.assertIsNotNone(app._route_path('/', 'GET')[1])
        self.assertIsNotNone(app._route_path('/', 'PUT')[1])
        self.assertIsNotNone(app._route_path('/', 'POST')[1])
        self.assertIsNotNone(app._route_path('/', 'DELETE')[1])

        # make sure the methods are all the same correct one
        self.assertIs(app._route_path('/', 'GET')[1][2], endware_1)
        self.assertIs(app._route_path('/', 'PUT')[1][2], endware_1)
        self.assertIs(app._route_path('/', 'POST')[1][2], endware_1)
        self.assertIs(app._route_path('/', 'DELETE')[1][2], endware_1)
Ejemplo n.º 6
0
    def test_use(self):
        app = App()

        app.use(middleware_1, path='/')

        router = Router()

        router.use('/', middleware_2)

        app.use(router, path='/abc')

        self.assertIsNotNone(app._route_path('/', 'GET')[0])

        self.assertEqual(len(app._route_path('/', 'GET')[0]),
                         2)  # two elements in the middleware
        self.assertIsNone(app._route_path('/',
                                          'GET')[1])  # no items in the endware

        self.assertTrue(middleware_1 in app._route_path(
            '/', 'GET')[0][0])  # middleware_1 in the first tuple in middleware

        self.assertTrue(middleware_2 in app._route_path(
            '/',
            'GET')[0][1])  # middleware_2 in the second tuple in middleware