"Fossies" - the Fresh Open Source Software Archive

Member "incubator-pagespeed-mod-1.14.36.1/pagespeed/system/admin_site_test.cc" (28 Feb 2020, 5007 Bytes) of package /linux/www/apache_httpd_modules/incubator-pagespeed-mod-1.14.36.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "admin_site_test.cc" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.13.35.2_vs_1.14.36.1.

    1 /*
    2  * Licensed to the Apache Software Foundation (ASF) under one
    3  * or more contributor license agreements.  See the NOTICE file
    4  * distributed with this work for additional information
    5  * regarding copyright ownership.  The ASF licenses this file
    6  * to you under the Apache License, Version 2.0 (the
    7  * "License"); you may not use this file except in compliance
    8  * with the License.  You may obtain a copy of the License at
    9  * 
   10  *   http://www.apache.org/licenses/LICENSE-2.0
   11  * 
   12  * Unless required by applicable law or agreed to in writing,
   13  * software distributed under the License is distributed on an
   14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   15  * KIND, either express or implied.  See the License for the
   16  * specific language governing permissions and limitations
   17  * under the License.
   18  */
   19 
   20 // Unit tests for AdminSite.
   21 
   22 #include "pagespeed/system/admin_site.h"
   23 
   24 #include "net/instaweb/http/public/async_fetch.h"
   25 #include "net/instaweb/rewriter/public/custom_rewrite_test_base.h"
   26 #include "net/instaweb/rewriter/public/rewrite_driver.h"
   27 #include "net/instaweb/rewriter/public/rewrite_driver_factory.h"
   28 #include "net/instaweb/rewriter/public/rewrite_test_base.h"
   29 #include "net/instaweb/rewriter/public/server_context.h"
   30 #include "net/instaweb/rewriter/public/test_rewrite_driver_factory.h"
   31 #include "pagespeed/system/system_rewrite_options.h"
   32 #include "pagespeed/system/system_server_context.h"
   33 #include "pagespeed/kernel/base/gmock.h"
   34 #include "pagespeed/kernel/base/gtest.h"
   35 #include "pagespeed/kernel/base/message_handler.h"
   36 #include "pagespeed/kernel/base/mock_message_handler.h"
   37 #include "pagespeed/kernel/base/scoped_ptr.h"
   38 #include "pagespeed/kernel/base/string.h"
   39 #include "pagespeed/kernel/base/thread_system.h"
   40 #include "pagespeed/kernel/util/platform.h"
   41 
   42 namespace net_instaweb {
   43 
   44 namespace {
   45 
   46 class SystemServerContextNoProxyHtml : public SystemServerContext {
   47  public:
   48   explicit SystemServerContextNoProxyHtml(RewriteDriverFactory* factory)
   49       : SystemServerContext(factory, "fake_hostname", 80 /* fake port */) {
   50   }
   51 
   52   virtual bool ProxiesHtml() const { return false; }
   53 
   54  private:
   55   DISALLOW_COPY_AND_ASSIGN(SystemServerContextNoProxyHtml);
   56 };
   57 
   58 class AdminSiteTest : public CustomRewriteTestBase<SystemRewriteOptions> {
   59  protected:
   60   AdminSiteTest()
   61       : thread_system_(Platform::CreateThreadSystem()),
   62         options_(new SystemRewriteOptions(thread_system_.get())),
   63         admin_site_(new AdminSite(factory()->static_asset_manager(), timer(),
   64                                   message_handler())) {
   65   }
   66 
   67   virtual void SetUp() {
   68     CustomRewriteTestBase<SystemRewriteOptions>::SetUp();
   69     server_context_.reset(SetupServerContext(options_.release()));
   70   }
   71 
   72   virtual void TearDown() {
   73     RewriteTestBase::TearDown();
   74   }
   75 
   76   // Set up the ServerContext. The ServerContext is only used for
   77   // PrintCaches method. If we remove this dependency later,
   78   // we don't need to set up ServerContext in this unit test.
   79   ServerContext* SetupServerContext(SystemRewriteOptions* config) {
   80     scoped_ptr<SystemServerContext> server_context(
   81         new SystemServerContextNoProxyHtml(factory()));
   82     server_context->reset_global_options(config);
   83     server_context->set_statistics(factory()->statistics());
   84     return server_context.release();
   85   }
   86 
   87   scoped_ptr<ThreadSystem> thread_system_;
   88   scoped_ptr<ServerContext> server_context_;
   89   scoped_ptr<SystemRewriteOptions> options_;
   90   scoped_ptr<AdminSite> admin_site_;
   91 };
   92 
   93 TEST_F(AdminSiteTest, ColorMessagesInHistoryPage) {
   94   EXPECT_EQ(message_handler(), admin_site_->MessageHandlerForTesting());
   95   // Due to the size limit to the SharedCircularBuffer, the earliest message
   96   // in the buffer may be incomplete. In order to always display complete
   97   // messages on the history page, we simply ignore all the things before the
   98   // first new line. So here we inject a useless line at the beginning to show
   99   // that we throw out the first (possibly) incomplete line.
  100   message_handler()->Message(kInfo, "Ignore the first line.");
  101   message_handler()->Message(kError, "Test for %s", "Errors");
  102   message_handler()->Message(kWarning, "Test for %s", "Warnings");
  103   message_handler()->Message(kInfo, "Test for %s", "Infos");
  104   GoogleString buffer;
  105   StringAsyncFetch fetch(rewrite_driver()->request_context(), &buffer);
  106   static const char kColorTemplate[] = "color:%s; margin:0;";
  107   // The value of the first argument AdminSite::AdminSource
  108   // does not matter in this test. So we just test for kPageSpeedAdmin here.
  109   admin_site_->MessageHistoryHandler(*(rewrite_driver()->options()),
  110                                      AdminSite::kPageSpeedAdmin, &fetch);
  111   EXPECT_THAT(
  112       buffer, ::testing::HasSubstr(StringPrintf(kColorTemplate, "red")));
  113   EXPECT_THAT(
  114       buffer, ::testing::HasSubstr(StringPrintf(kColorTemplate, "brown")));
  115   EXPECT_THAT(buffer, ::testing::HasSubstr("style=\"margin:0;\""));
  116 }
  117 // TODO(xqyin): Add unit tests for other methods in AdminSite.
  118 
  119 }  // namespace
  120 
  121 }  // namespace net_instaweb