PageRenderTime 34ms CodeModel.GetById 12ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

/mordor/tests/http_servlet_dispatcher.cpp

http://github.com/mozy/mordor
C++ | 118 lines | 90 code | 18 blank | 10 comment | 0 complexity | cda4434a5d006ab8b199bd3c50ecaa7c MD5 | raw file
  1// Copyright (c) 2010 - Mozy, Inc.
  2
  3#include "mordor/http/servlet.h"
  4#include "mordor/test/test.h"
  5
  6using namespace Mordor;
  7using namespace Mordor::HTTP;
  8
  9namespace {
 10class DummyServlet : public Servlet
 11{
 12public:
 13    void request(boost::shared_ptr<ServerRequest> request) {}
 14};
 15}
 16
 17MORDOR_UNITTEST(ServletDispatcher, basic)
 18{
 19    ServletDispatcher dispatcher;
 20    Servlet::ptr root(new DummyServlet), ab(new DummyServlet),
 21        ca(new DummyServlet), ma(new DummyServlet), wl(new DummyServlet);
 22
 23    // non registered, can't find any
 24    MORDOR_TEST_ASSERT(!dispatcher.getServlet("/d/e/f"));
 25
 26    dispatcher.registerServlet("/a/b", ab);
 27    dispatcher.registerServlet("/c/a", ca);
 28    dispatcher.registerServlet("/m/a/", ma);
 29    dispatcher.registerServlet("/w/*/l/", wl);
 30
 31    // some servlets registered, still can't find it
 32    MORDOR_TEST_ASSERT(!dispatcher.getServlet("/d/e/f"));
 33
 34    dispatcher.registerServlet("/", root);
 35    MORDOR_TEST_ASSERT_EQUAL(dispatcher.getServlet("/d/e/f"), root);
 36    MORDOR_TEST_ASSERT_EQUAL(dispatcher.getServlet("/a/b") , ab);
 37    MORDOR_TEST_ASSERT_EQUAL(dispatcher.getServlet("/a/b/") , ab);
 38    MORDOR_TEST_ASSERT_EQUAL(dispatcher.getServlet("/a/b/c") , ab);
 39    MORDOR_TEST_ASSERT_EQUAL(dispatcher.getServlet("/a/b/c/d") , ab);
 40    MORDOR_TEST_ASSERT_EQUAL(dispatcher.getServlet("/A/B") , root);
 41    MORDOR_TEST_ASSERT_EQUAL(dispatcher.getServlet("/a") , root);
 42    MORDOR_TEST_ASSERT_EQUAL(dispatcher.getServlet("/a/bc") , root);
 43    MORDOR_TEST_ASSERT_EQUAL(dispatcher.getServlet("/m/a/b/c") , ma);
 44    MORDOR_TEST_ASSERT_EQUAL(dispatcher.getServlet("/m/a/") , ma);
 45    MORDOR_TEST_ASSERT_EQUAL(dispatcher.getServlet("/m/a") , root);
 46    MORDOR_TEST_ASSERT_EQUAL(dispatcher.getServlet("/w/*/l/") , wl);
 47    // can only match the root
 48    MORDOR_TEST_ASSERT_EQUAL(dispatcher.getServlet("/w/a/l/") , root);
 49    // Dispatcher does URI normalization
 50    MORDOR_TEST_ASSERT_EQUAL(dispatcher.getServlet("/a/b/../../c/a"), ca);
 51}
 52
 53MORDOR_UNITTEST(ServletDispatcher, vhosts)
 54{
 55    ServletDispatcher dispatcher;
 56    Servlet::ptr root(new DummyServlet), trogdor(new DummyServlet),
 57        mordor(new DummyServlet);
 58
 59    dispatcher.registerServlet("http://trogdor/", trogdor);
 60    dispatcher.registerServlet("//mordor/", mordor);
 61
 62    MORDOR_TEST_ASSERT(!dispatcher.getServlet("/"));
 63    MORDOR_TEST_ASSERT(!dispatcher.getServlet("//triton/"));
 64    MORDOR_TEST_ASSERT_EQUAL(dispatcher.getServlet("//trogdor/a/b") , trogdor);
 65    MORDOR_TEST_ASSERT_EQUAL(dispatcher.getServlet("//mordor/a/b") , mordor);
 66    MORDOR_TEST_ASSERT_EQUAL(dispatcher.getServlet("http://trogdor/a/b") , trogdor);
 67    MORDOR_TEST_ASSERT_EQUAL(dispatcher.getServlet("http://mordor/a/b") , mordor);
 68
 69    dispatcher.registerServlet("/", root);
 70    MORDOR_TEST_ASSERT_EQUAL(dispatcher.getServlet("/") , root);
 71    MORDOR_TEST_ASSERT_EQUAL(dispatcher.getServlet("//triton/") , root);
 72    MORDOR_TEST_ASSERT_EQUAL(dispatcher.getServlet("//trogdor/a/b") , trogdor);
 73    MORDOR_TEST_ASSERT_EQUAL(dispatcher.getServlet("//mordor/a/b") , mordor);
 74}
 75
 76MORDOR_UNITTEST(ServletDispatcher, wildcardBasic)
 77{
 78    ServletDispatcher dispatcher(true);
 79    Servlet::ptr axb(new DummyServlet), cxdx(new DummyServlet);
 80
 81    dispatcher.registerServlet("/a/*/b", axb);
 82    dispatcher.registerServlet("/c/*/d/*/", cxdx);
 83    MORDOR_TEST_ASSERT_EQUAL(dispatcher.getServlet("/a/b/b"), axb);
 84    MORDOR_TEST_ASSERT_EQUAL(dispatcher.getServlet("/a/cc/b"), axb);
 85    MORDOR_TEST_ASSERT_EQUAL(dispatcher.getServlet("/a/cc/b/d"), axb);
 86    MORDOR_TEST_ASSERT_EQUAL(dispatcher.getServlet("/c/y/d/m/n"), cxdx);
 87    MORDOR_TEST_ASSERT_EQUAL(dispatcher.getServlet("/a/b/b/b"), axb);
 88    // not match
 89    MORDOR_TEST_ASSERT(!dispatcher.getServlet("/a/b/c"));
 90    MORDOR_TEST_ASSERT(!dispatcher.getServlet("/b/*/b"));
 91}
 92
 93MORDOR_UNITTEST(ServletDispatcher, wildcardLowerPriority)
 94{
 95    ServletDispatcher dispatcher(true);
 96    Servlet::ptr axb(new DummyServlet), abb(new DummyServlet);
 97
 98    dispatcher.registerServlet("/a/*/b", axb);
 99    dispatcher.registerServlet("/a/b/b", abb);
100    // both match, wildcard has lower priority
101    MORDOR_TEST_ASSERT_EQUAL(dispatcher.getServlet("/a/b/b/c"), abb);
102    MORDOR_TEST_ASSERT_EQUAL(dispatcher.getServlet("/a/c/b/c"), axb);
103}
104
105MORDOR_UNITTEST(ServletDispatcher, wildcardMatchSeqOrder)
106{
107    ServletDispatcher dispatcher(true);
108    Servlet::ptr axb(new DummyServlet), acx(new DummyServlet);
109
110    dispatcher.registerServlet("/a/*/b", axb);
111    dispatcher.registerServlet("/a/c/*", acx);
112    // only axb match
113    MORDOR_TEST_ASSERT_EQUAL(dispatcher.getServlet("/a/x/b/c"), axb);
114    // only acx match
115    MORDOR_TEST_ASSERT_EQUAL(dispatcher.getServlet("/a/c/x/c"), acx);
116    // both match, the one who has the wildcard in the most right win
117    MORDOR_TEST_ASSERT_EQUAL(dispatcher.getServlet("/a/c/b/d"), acx);
118}