def testFetchInvalidUrl(self):
     self.assertEquals({'fetch_impossible': True},
                       metadata_fetch.FetchAndUpdateMetadata(
                           None, 'WMS:blarg'))
     self.assertEquals({'fetch_impossible': True},
                       metadata_fetch.FetchAndUpdateMetadata(
                           None, 'KML:blarg:'))
     self.assertEquals({'fetch_impossible': True},
                       metadata_fetch.FetchAndUpdateMetadata(
                           None, 'KML:blarg://'))
    def testFetchWithEtag(self):
        # Verify that we send "If-none-match", and simulate getting a 304.
        self.mox.StubOutWithMock(urlfetch, 'fetch')
        headers = {'If-none-match': ETAG}
        urlfetch.fetch(SOURCE_URL, headers=headers, deadline=30).AndReturn(
            utils.Struct(status_code=304, headers={}, content='Not modified'))

        self.mox.ReplayAll()
        # Pretend there is existing metadata for 1234 bytes of content.
        old_metadata = {
            'fetch_status': 200,
            'fetch_length': 1234,
            'fetch_etag': ETAG,
            'length': 1234,
            'md5_hash': 'foo'
        }
        # Updated metadata should be the same except fetch_status and fetch_length.
        self.assertEquals(
            {
                'fetch_status': 304,
                'fetch_length': len('Not modified'),
                'fetch_etag': ETAG,
                'length': 1234,
                'md5_hash': 'foo'
            },
            metadata_fetch.FetchAndUpdateMetadata(old_metadata,
                                                  SOURCE_ADDRESS))

        self.mox.VerifyAll()
    def testFetchSecondTime(self):
        # Simulate a successful fetch of a document that was previously fetched.
        self.mox.StubOutWithMock(urlfetch, 'fetch')
        headers = {'If-none-match': ETAG}
        urlfetch.fetch(SOURCE_URL, headers=headers, deadline=30).AndReturn(
            utils.Struct(status_code=200,
                         headers=RESPONSE_HEADERS_2,
                         content=SIMPLE_KML_2))

        self.mox.ReplayAll()
        self.assertEquals(
            {
                'fetch_status': 200,
                'fetch_length': len(SIMPLE_KML_2),
                'fetch_last_modified': LAST_MODIFIED_STRING_2,
                'fetch_etag': ETAG_2,
                'update_time': LAST_MODIFIED_TIMESTAMP_2,
                'length': len(SIMPLE_KML_2),
                'md5_hash': hashlib.md5(SIMPLE_KML_2).hexdigest()
            },
            metadata_fetch.FetchAndUpdateMetadata(
                {
                    'fetch_status': 200,
                    'fetch_length': len(SIMPLE_KML),
                    'fetch_last_modified': LAST_MODIFIED_STRING,
                    'fetch_etag': ETAG,
                    'update_time': LAST_MODIFIED_TIMESTAMP,
                    'length': len(SIMPLE_KML),
                    'md5_hash': hashlib.md5(SIMPLE_KML).hexdigest()
                }, SOURCE_ADDRESS))

        self.mox.VerifyAll()
    def testFetchDownloadError(self):
        # Simulate a DownloadError.
        self.mox.StubOutWithMock(urlfetch, 'fetch')
        urlfetch.fetch(SOURCE_URL, headers={}, deadline=30).AndRaise(
            urlfetch_errors.DownloadError('the internets are down'))

        self.mox.ReplayAll()
        self.assertEquals({'fetch_error_occurred': True},
                          metadata_fetch.FetchAndUpdateMetadata(
                              None, SOURCE_ADDRESS))

        self.mox.VerifyAll()
    def testUpdateMetadata(self):
        self.mox.StubOutWithMock(metadata_fetch, 'FetchAndUpdateMetadata')
        metadata_fetch.FetchAndUpdateMetadata(
            METADATA, SOURCE_ADDRESS).AndReturn(METADATA_2)

        self.mox.ReplayAll()
        self.SetTime(1234567890)
        metadata_fetch.METADATA_CACHE.Set(SOURCE_ADDRESS, METADATA)
        metadata_fetch.UpdateMetadata(SOURCE_ADDRESS)
        self.assertEquals(METADATA_2,
                          metadata_fetch.METADATA_CACHE.Get(SOURCE_ADDRESS))

        self.mox.VerifyAll()
    def testFetchHttpError(self):
        # Simulate a 404 Not found error.
        self.mox.StubOutWithMock(urlfetch, 'fetch')
        urlfetch.fetch(SOURCE_URL, headers={}, deadline=30).AndReturn(
            utils.Struct(status_code=404, headers={}, content='Not found'))

        self.mox.ReplayAll()
        self.assertEquals({
            'fetch_error_occurred': True,
            'fetch_status': 404
        }, metadata_fetch.FetchAndUpdateMetadata(None, SOURCE_ADDRESS))

        self.mox.VerifyAll()
    def testFetchFirstTime(self):
        # Simulate a normal, successful fetch of a document for the first time.
        self.mox.StubOutWithMock(urlfetch, 'fetch')
        urlfetch.fetch(SOURCE_URL, headers={}, deadline=30).AndReturn(
            utils.Struct(status_code=200,
                         headers=RESPONSE_HEADERS,
                         content=SIMPLE_KML))

        self.mox.ReplayAll()
        self.assertEquals(
            {
                'fetch_status': 200,
                'fetch_length': len(SIMPLE_KML),
                'fetch_last_modified': LAST_MODIFIED_STRING,
                'fetch_etag': ETAG,
                'update_time': LAST_MODIFIED_TIMESTAMP,
                'length': len(SIMPLE_KML),
                'md5_hash': hashlib.md5(SIMPLE_KML).hexdigest()
            }, metadata_fetch.FetchAndUpdateMetadata(None, SOURCE_ADDRESS))

        self.mox.VerifyAll()