15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_request_job.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
70f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "net/base/request_priority.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/http/http_transaction_unittest.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_request_test_util.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
120f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)namespace net {
130f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This is a header that signals the end of the data.
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const char kGzipGata[] = "\x1f\x08b\x08\0\0\0\0\0\0\3\3\0\0\0\0\0\0\0\0";
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
190f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)void GZipServer(const HttpRequestInfo* request,
200f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                std::string* response_status,
210f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                std::string* response_headers,
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                std::string* response_data) {
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  response_data->assign(kGzipGata, sizeof(kGzipGata));
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const MockTransaction kGZip_Transaction = {
270f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    "http://www.google.com/gzyp",
280f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    "GET",
290f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    base::Time(),
300f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    "",
310f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    LOAD_NORMAL,
320f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    "HTTP/1.1 200 OK",
330f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    "Cache-Control: max-age=10000\n"
340f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    "Content-Encoding: gzip\n"
350f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    "Content-Length: 30\n",  // Intentionally wrong.
360f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    base::Time(),
370f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    "",
380f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    TEST_MODE_NORMAL,
390f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    &GZipServer,
400f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    0,
410f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    OK
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
44d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)const MockTransaction kRedirect_Transaction = {
450f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    "http://www.google.com/redirect",
460f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    "GET",
470f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    base::Time(),
480f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    "",
490f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    LOAD_NORMAL,
500f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    "HTTP/1.1 302 Found",
510f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    "Cache-Control: max-age=10000\n"
520f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    "Location: http://www.google.com/destination\n"
530f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    "Content-Length: 5\n",
540f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    base::Time(),
550f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    "hello",
560f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    TEST_MODE_NORMAL,
570f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    NULL,
580f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    0,
590f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    OK
60d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)};
61d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST(URLRequestJob, TransactionNotifiedWhenDone) {
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockNetworkLayer network_layer;
660f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  TestURLRequestContext context;
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  context.set_http_transaction_factory(&network_layer);
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
690f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  TestDelegate d;
700f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  TestURLRequest req(
710f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      GURL(kGZip_Transaction.url), DEFAULT_PRIORITY, &d, &context);
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AddMockTransaction(&kGZip_Transaction);
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  req.set_method("GET");
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  req.Start();
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  base::MessageLoop::current()->Run();
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(network_layer.done_reading_called());
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RemoveMockTransaction(&kGZip_Transaction);
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST(URLRequestJob, SyncTransactionNotifiedWhenDone) {
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockNetworkLayer network_layer;
860f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  TestURLRequestContext context;
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  context.set_http_transaction_factory(&network_layer);
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
890f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  TestDelegate d;
900f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  TestURLRequest req(
910f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      GURL(kGZip_Transaction.url), DEFAULT_PRIORITY, &d, &context);
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockTransaction transaction(kGZip_Transaction);
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  transaction.test_mode = TEST_MODE_SYNC_ALL;
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AddMockTransaction(&transaction);
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  req.set_method("GET");
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  req.Start();
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  base::MessageLoop::current()->Run();
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(network_layer.done_reading_called());
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RemoveMockTransaction(&transaction);
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
105d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
106d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)TEST(URLRequestJob, RedirectTransactionNotifiedWhenDone) {
107d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  MockNetworkLayer network_layer;
1080f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  TestURLRequestContext context;
109d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  context.set_http_transaction_factory(&network_layer);
110d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
1110f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  TestDelegate d;
1120f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  TestURLRequest req(
1130f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      GURL(kRedirect_Transaction.url), DEFAULT_PRIORITY, &d, &context);
114d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  AddMockTransaction(&kRedirect_Transaction);
115d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
116d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  req.set_method("GET");
117d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  req.Start();
118d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
119d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  base::MessageLoop::current()->Run();
120d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
121d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  EXPECT_TRUE(network_layer.done_reading_called());
122d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
123d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  RemoveMockTransaction(&kRedirect_Transaction);
124d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
1250f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1260f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}  // namespace net
127