📄 httpexportresponsedecoder.cpp
字号:
//%2006//////////////////////////////////////////////////////////////////////////// Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development// Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.// Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;// IBM Corp.; EMC Corporation, The Open Group.// Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;// IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.// Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;// EMC Corporation; VERITAS Software Corporation; The Open Group.// Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;// EMC Corporation; Symantec Corporation; The Open Group.//// Permission is hereby granted, free of charge, to any person obtaining a copy// of this software and associated documentation files (the "Software"), to// deal in the Software without restriction, including without limitation the// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or// sell copies of the Software, and to permit persons to whom the Software is// furnished to do so, subject to the following conditions:// // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN// ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.////==============================================================================////%/////////////////////////////////////////////////////////////////////////////#include <Pegasus/Common/Config.h>#include <Pegasus/Common/PegasusAssert.h>#include <Pegasus/Common/StrLit.h>#include <Pegasus/Client/CIMClientException.h>#include <Pegasus/ExportClient/HTTPExportResponseDecoder.h>PEGASUS_USING_PEGASUS;PEGASUS_USING_STD;const CIMName MALFORMED_HTTP = CIMName ("CIMClientMalformedHTTPException");const CIMName HTTP_ERROR = CIMName ("CIMClientHTTPErrorException");const CIMName XML_EXCEPTION = CIMName ("CIMClientXmlException");const CIMName RESPONSE_EXCEPTION = CIMName ("CIMClientResponseException");void testParseHTTPHeaders( Buffer httpBuffer, Boolean expectedValid, Boolean expectedReconnect, Uint32 expectedType){ ClientExceptionMessage* exceptionMessage; Array<HTTPHeader> headers; Uint32 contentLength; Uint32 statusCode; String reasonPhrase; Boolean cimReconnect; Boolean valid; HTTPMessage* httpMessage = new HTTPMessage(httpBuffer); HTTPExportResponseDecoder::parseHTTPHeaders(httpMessage, exceptionMessage, headers, contentLength, statusCode, reasonPhrase, cimReconnect, valid); PEGASUS_TEST_ASSERT(valid == expectedValid); PEGASUS_TEST_ASSERT(cimReconnect == expectedReconnect); PEGASUS_TEST_ASSERT(exceptionMessage->getType() == expectedType); CIMClientMalformedHTTPException* malformedHTTPException = dynamic_cast<CIMClientMalformedHTTPException*>( exceptionMessage->clientException); PEGASUS_TEST_ASSERT (malformedHTTPException);}void testValidateHTTPHeaders( Buffer httpBuffer, Boolean expectedValid, Boolean expectedReconnect, Uint32 expectedType, CIMName expectedClass){ ClientExceptionMessage* exceptionMessage; Array<HTTPHeader> headers; char* content; Uint32 contentLength; Uint32 statusCode; String reasonPhrase; Boolean cimReconnect; Boolean valid; HTTPMessage* httpMessage = new HTTPMessage(httpBuffer); HTTPExportResponseDecoder::parseHTTPHeaders(httpMessage, exceptionMessage, headers, contentLength, statusCode, reasonPhrase, cimReconnect, valid); HTTPExportResponseDecoder::validateHTTPHeaders(httpMessage, headers, contentLength, statusCode, cimReconnect, reasonPhrase, content, exceptionMessage, valid); PEGASUS_TEST_ASSERT(valid == expectedValid); PEGASUS_TEST_ASSERT(cimReconnect == expectedReconnect); PEGASUS_TEST_ASSERT(exceptionMessage->getType() == expectedType); if (expectedClass == MALFORMED_HTTP) { CIMClientMalformedHTTPException* malformedHTTPException = dynamic_cast<CIMClientMalformedHTTPException*>( exceptionMessage->clientException); PEGASUS_TEST_ASSERT (malformedHTTPException); } else if (expectedClass == HTTP_ERROR) { CIMClientHTTPErrorException* httpErrorException = dynamic_cast<CIMClientHTTPErrorException*>( exceptionMessage->clientException); PEGASUS_TEST_ASSERT (httpErrorException); }}void testDecodeExportResponse( Buffer httpBuffer, Uint32 expectedType, // expectedClass is used only if expectedType is ClientExceptionMessage // otherwise, it is ignored CIMName expectedClass){ ClientExceptionMessage* exceptionMessage; Array<HTTPHeader> headers; char* content; Uint32 contentLength; Uint32 statusCode; String reasonPhrase; Boolean cimReconnect; Boolean valid; Message* responseMessage; HTTPMessage* httpMessage = new HTTPMessage(httpBuffer); HTTPExportResponseDecoder::parseHTTPHeaders(httpMessage, exceptionMessage, headers, contentLength, statusCode, reasonPhrase, cimReconnect, valid); HTTPExportResponseDecoder::validateHTTPHeaders(httpMessage, headers, contentLength, statusCode, cimReconnect, reasonPhrase, content, exceptionMessage, valid); HTTPExportResponseDecoder::decodeExportResponse(content, cimReconnect, responseMessage); PEGASUS_TEST_ASSERT(responseMessage->getType() == expectedType); if (responseMessage->getType() == CLIENT_EXCEPTION_MESSAGE) { if (expectedClass == XML_EXCEPTION) { CIMClientXmlException* xmlException = dynamic_cast<CIMClientXmlException*>(((ClientExceptionMessage*) responseMessage)->clientException); PEGASUS_TEST_ASSERT (xmlException); } else if (expectedClass == RESPONSE_EXCEPTION) { CIMClientResponseException* responseException = dynamic_cast<CIMClientResponseException*>( ((ClientExceptionMessage*) responseMessage)->clientException); PEGASUS_TEST_ASSERT (responseException); } }}////////////////////////////////////////////////////////////////// //// Test Cases for parseHTTPHeaders method //// //////////////////////////////////////////////////////////////////void parseHTTPHeadersTestCases(){ // // Test empty HTTP response message // // Expected result: // valid is false // cimReconnect is false // exceptionMessage type is CLIENT_EXCEPTION_MESSAGE (75) // clientException is CIMClientMalformedHTTPException // Buffer buffer1(STRLIT_ARGS("")); testParseHTTPHeaders(buffer1, false, false, CLIENT_EXCEPTION_MESSAGE); // // Test HTTP response message including Connection: Close header // and a non-parsable status line // // Expected result: // valid is false // cimReconnect is true // exceptionMessage type is CLIENT_EXCEPTION_MESSAGE (75) // clientException is CIMClientMalformedHTTPException // Buffer buffer2(STRLIT_ARGS( "HTTP/1.1200OK\r\n" "Connection: Close\r\n")); testParseHTTPHeaders(buffer2, false, true, CLIENT_EXCEPTION_MESSAGE); // // Test HTTP response message including Connection: header // with a connection-token other than "Close" // and a non-parsable status line // // Expected result: // valid is false // cimReconnect is false // exceptionMessage type is CLIENT_EXCEPTION_MESSAGE (75) // clientException is CIMClientMalformedHTTPException // Buffer buffer3(STRLIT_ARGS( "HTTP/1.1200OK\r\n" "Connection: Keep-Alive\r\n")); testParseHTTPHeaders(buffer3, false, false, CLIENT_EXCEPTION_MESSAGE);}////////////////////////////////////////////////////////////////// //// Test Cases for validateHTTPHeaders method //// //////////////////////////////////////////////////////////////////void validateHTTPHeadersTestCases(){ // // Test HTTP response message with non-success status // // Expected result: // valid is false // cimReconnect is false // exceptionMessage type is CLIENT_EXCEPTION_MESSAGE (75) // clientException is CIMClientHTTPErrorException // Buffer buffer4(STRLIT_ARGS( "HTTP/1.1 501 Not Implemented\r\n" "CIMError: multiple-requests-unsupported\r\n")); testValidateHTTPHeaders(buffer4, false, false, CLIENT_EXCEPTION_MESSAGE, HTTP_ERROR); // // Test HTTP response message with non-success status // and invalid URI encoding in error // // Expected result: // valid is false // cimReconnect is false // exceptionMessage type is CLIENT_EXCEPTION_MESSAGE (75) // clientException is CIMClientHTTPErrorException // Buffer buffer5(STRLIT_ARGS( "HTTP/1.1 400 Bad Request\r\n" "PGErrorDetail: HTTP%20request%\r\n")); testValidateHTTPHeaders(buffer5, false, false, CLIENT_EXCEPTION_MESSAGE, HTTP_ERROR); // // Test HTTP response message with missing CIMExport HTTP header // // Expected result: // valid is false // cimReconnect is false // exceptionMessage type is CLIENT_EXCEPTION_MESSAGE (75) // clientException is CIMClientMalformedHTTPException // Buffer buffer6(STRLIT_ARGS( "HTTP/1.1 200 OK\r\n" "Content-Type: application/xml; charset=\"utf-8\"\r\n" "transfer-encoding: chunked\r\n" "Trailer: CIMStatusCode, CIMStatusCodeDescription, " "Content-Language\r\n" "\r\n" "<?xml version=\"1.0\" encoding=\"utf-8\"?>" "<CIM CIMVERSION=\"2.0\" DTDVERSION=\"2.0\">" "<MESSAGE ID=\"1001\" PROTOCOLVERSION=\"1.0\">" "<SIMPLEEXPRSP>" "<EXPMETHODRESPONSE NAME=\"ExportIndication\">" "<ERROR CODE=\"7\" DESCRIPTION=\"CIM_ERR_NOT_SUPPORTED: " "The requested operation is not supported\"/>" "</EXPMETHODRESPONSE>" "</SIMPLEEXPRSP>" "</MESSAGE>" "</CIM>")); testValidateHTTPHeaders(buffer6, false, false,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -