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

WFRxHttpServerTestHandlers.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2019 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.

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

local Rx = require "Rx"

local KSPathUtils = require "KSPathUtils"

local WFRxHttpServerUtils = require "WFRxHttpServerUtils"

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

local handlers = {

	{ '^/docValue/(%w+)$', { 'GET' }, function( path, info, catalog )
	
		return catalog:docStore():docValue( info.matches[ 1 ] )
			:map( function( docValue )
				return { contentType = 'text/plain', data = docValue.content }	
			end )
			
	end },
	 { '^/sendFile/(.+)$', { 'GET' }, function( path, info, catalog )

		local testDataPath = KSPathUtils.getStandardFilePath( "testdata" )
		testDataPath = KSPathUtils.child( testDataPath, 'assets' )

		return Rx.Observable.just{ 
			contentType = 'image/jpg', 
			filePath = KSPathUtils.child( testDataPath, info.matches[ 1 ] )  }	
	
	end },
	{ '^/(%a+)/(%d+)$', { 'GET' }, function( path, info, catalog )
	
		if info.matches[ 1 ] and info.matches[ 2 ] then
			return Rx.Observable.just{ contentType = 'text/plain', data = info.matches[ 1 ] .. info.matches[ 2 ] }		
		else
			return Rx.Observable.just{ statusCode  = 404, data = 'not found' }
		end
		
	end },
	{ '^/put$', { 'PUT' }, function( path, info, catalog )

		return Rx.Observable.just{ contentType = 'text/plain', data = info.postData }

	end },
	{ '^/error$', { 'GET' }, function( catalog, path, info )

		return Rx.Observable.just{ statusCode = 404, data = 'silly path, mate' }

	end },
	{ '^/$', { 'GET' }, function( path, info, catalog )
	
		return Rx.Observable.just{ contentType = 'text/plain', data = 'Hello, Wichita!\n' }	

	end },

}

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

local WFRxHttpServerTestHandlers = {}

function WFRxHttpServerTestHandlers.startRequest( uri, requestParams, ... )

	return { 
		value = WFRxHttpServerUtils.startRequest( handlers, uri, requestParams, ... )
	}

end

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


return WFRxHttpServerTestHandlers