--[[----------------------------------------------------------------------------

WFRxHttpCancellationTest.lua

--------------------------------------------------------------------------------

ADOBE CONFIDENTIAL
------------------
Copyright 2016 Adobe Systems Incorporated
All Rights Reserved.

NOTICE: All information contained herein is, and remain the property of
Adobe Systems Incorporated and its suppliers, if any. The intellectual and
technical concepts contained herein are proprietary to Adobe Systems
Incorporated and its suppliers and may be covered by U.S. and Foreign
Patents, patents in process, and are protected by trade secret or copyright
law.  Dissemination of this information or reproduction of this material is
strictly forbidden unless prior written permission is obtained from
Adobe Systems Incorporated.

------------------------------------------------------------------------------]]

	-- RxLua
local WFRxTestCase = require 'WFRxTestCase'

	-- WF.web.http_client
local WFRxHttpClient = require 'WFRxHttpClient'

local KSPathUtils = require 'KSPathUtils'
local KSData = require 'KSData'
local WFPlatformInfo = require 'WFPlatformInfo'

--============================================================================--

local WFRxHttpCancellationTest = {}

local addr = "http://kibler.corp.adobe.com:8179/"

--------------------------------------------------------------------------------

if not WFPlatformInfo.isAndroid and not WFPlatformInfo.isWindows then

-- TODO [kcarlson 2016-08-25]: Add cancelation support to android.
-- FIXME [jandrew 2018-03-30]: Disabling this test on Windows because it's unstable.

function WFRxHttpCancellationTest.rxTestCancelRequest_testNightly( rxTest )

	rxTest:setTimeLimit( 30 )

	local testDataFolder = KSPathUtils.getStandardFilePath( 'testdata' )
	local filePath = testDataFolder .. '/rtmfp/1MB.txt'
	local file = io.open( filePath, 'r' )
	local content = file:read("*all")
    file:close()

	local largeData = ''
	
	for i = 1, 100 do
		largeData = largeData .. content
	end

	local sizeOfLargeData = KSData.length( KSData.make( largeData ) )

	-- Verify the server returns the last message is received.
	-- That is: POST -> Hello should result in GET -> Hello

	rxTest:expectSingleValue( WFRxHttpClient.put( addr, 'Hello', {} ), ':ignore:', ':ignore:' )

	rxTest:expectSingleValue( WFRxHttpClient.get( addr, {} )
			:map( function( data, headers )
				return tostring( data )
			end ), 'Hello' )
	
	-- Test Cancel

	local largeDataPut = 
		WFRxHttpClient.put(
			addr,
			largeData,
			{
				requestMethod = "POST",
				timeout = 100,
			} )
		:subscribe( function()
			assert( false )
		end )

	rxTest:sleep( 0.01 )
	
	-- Dispose of the large request
	
	largeDataPut:dispose()
	largeDataPut = nil

	collectgarbage()
	collectgarbage()
	collectgarbage()

	-- Check to make sure the server didn't receive the entire message
	
	local largeResponseLength
	
	rxTest:expectSingleValue( WFRxHttpClient.get( addr, {} )
			:map( function( data, headers )
				largeResponseLength = KSData.length( data )
				return largeResponseLength
			end ), ':ignore:' )
			
	rxTest:expectNotEqual( sizeOfLargeData, largeResponseLength )
			
	-- Sleep for long enough that the large transfer would have completed and
	-- then perform the same check.
			
	rxTest:sleep( 5 )
			
	rxTest:expectSingleValue( WFRxHttpClient.get( addr, {} )
			:map( function( data, headers )
				largeResponseLength = KSData.length( data )
				return largeResponseLength
			end ), ':ignore:' )
			
	rxTest:expectNotEqual( sizeOfLargeData, largeResponseLength )
	
end

end

--------------------------------------------------------------------------------

return WFRxTestCase.wrapSuite( WFRxHttpCancellationTest )