Mocking WebOperationContext - moq

I'm using the WCFMock to mock the WebOperationContext in my web service. The only usage is to add a custom HTTP header to the WebOperationContext.Current.OutgoingResponse.Headers collection. I'm unable to verify this using Moq. What I've already tried:
Verify if the Add method is getting invoked. This fails because Add is not virtual
Try to access the header directly from MockedWebOperationContext.Current. This is always zero in number
How can I verify in my unit test case that a custom header has been added?

Figured it out. Here's the solution for posterity.
When we create the "moq mock" for the IWebOperationContext, the example suggests that we set the property DefaultValue = DefaultValue.Mock. This will mock all dependincies including the HttpHeaders collection. I skipped this and mocked the OutgoingWebResponseContext to return a WebHeaderCollection. For my test case I simply assert on this collection.

Related

Parameter transfer to URI in SOAP UI for a Rest Service

I have two REST Services: GET and PATCH.
The GET Service has a JSON Response and from which I need to trasnfer a property named tripId.
Add that property value to the URL of the PATCH Request as a resouce, i.e.,
https://patchRequest.com/api/trips/{tripId}/
Can any one tell me how to do it in SOAP UI/ READY API.
I'm not able to do it.
Thanks
Create a customer property in the test case section ( or even in the project section )
Add a Property Transfer test step and export the value to the holder that you created
Finally, Call the value in your URI

Custom Header with token in PASOE Business Class Entity with Web Service?

I have a PASOE Business Class Entity setup as a Web Service. I'm trying to determine how to create a custom header that will allow me to pass in a hashed token. Is this something that I need to upgrade to 11.7.4 for DOH(OpenEdge.Web.DataObject.DataObjectHandler)? Or is this something that I simply add into a method that's defined in the class? Apologies, for the lack of code to illustrate my situation, but I'm not sure where to begin.
If you're using a Business Entity with the web transport then you're using the DOH, and the below applies. If you're using the rest transport then you are not using the DOH, and are more limited in your choices.
There is doc available on the DOH at https://documentation.progress.com/output/oe117sp/index.html#page/gssp4/openedge-data-object-handler.html - it's for 11.7.4 but largely applies to all versions (that is, from 11.6.3+). This describes the JSON mapping file, which you'll need to create an override to the default, generated one.
If you want to use the header's value for all operations, then you may want to use one of the DOH's events. There's an example of event handlers at https://github.com/PeterJudge-PSC/http_samples/blob/master/web_handler/data_object_handler/DOHEventHandler.cls ; you will need to start that handler in a session startup procedure using new DOHEventHandler() (the way that code is written is that it makes itself a singleton).
You can now add handling code for the Invoking event which fires before the business logic is run.
If you want to pass the header value into the business logic you will need to
Copy the generated mapping file <service>.gen to a <service.map> , in the same folder. "gen" files are generated and will be overwritten by the tooling
In the .map file, add a new arg entry. This must be in the same order as the parameters to the BE's method.
The JSON should look something like the below. this will read the value of the header and pass it as an input parameter into the method.
{ "ablName": "<parameter_name>",
"ablType": "CHARACTER",
"ioMode": "INPUT",
"msgElem": {"type": "HEADER", "name": "<http-header-name>"}
}

Application Insights Operation.Id from an HTTP header

My project uses ApplicationInsightsHttpModule which initializes Operation.Id from Microsoft.ApplicationInsights.RequestTelemetry HTTP value set by a client UI application. Now I want my API to be consumed by a third party which will provide X-Operation-Id HTTP header to correlate our activities. How do I make Application Insights to initialize Operation.Id from that header if it's present in a request?
This says that the standard context is managed automatically by AI so I need a code sample that shows how to properly initialize Operation.Id with a custom value. The following code isn't working, the header value is ignored:
var operationInitializer = TelemetryConfiguration.Active.TelemetryInitializers.OfType<Microsoft.ApplicationInsights.Web.OperationCorrelationTelemetryInitializer>().FirstOrDefault();
if (operationInitializer != null)
{
operationInitializer.RootOperationIdHeaderName = "X-Operation-Id";
}
The code that you included in your question should be working at first glance: you are looking for correct initializer and overriding correct property. However there could be multiple reasons why it is not working as expected, and debugging this is hard, because it would require you to actually step into Application Insights code (if you do decide, the sources for every release are archived on GH).
My recommendation in your case would be to take a different approach - instead of trying to override this property, create and register your own telemetry initializer to set Operation.ID from your header. If you set this property, OperationCorrelationTelemetryInitializer will ignore it and not override. This way you confirm that your headers can be read correctly, and you can easily set breakpoint in Initialize method to debug.
At the moment the question was asked AI SDK had version 2.2. Recently I've upgraded to the latest version 2.4 and it's working now as intended. So, if you pass say 'HELLO' in X-Operation-Id HTTP header (you still need that code in the question in Application_Start()) and then ask for a telemetry in Application_BeginRequest() (or wherever HttpContext is available):
RequestTelemetry telemetry = HttpContext.Current.GetRequestTelemetry();
telemetry.Id will contain something like |HELLO.d0b9d5b7_.
That hexadecimal suffix increases if you pass 'HELLO' in the next request so Id is always unique. I don't know if that can be overridden though.

setAttribute() and getParameter(), is this right?

I'm working on an servlet based project where I found that in a place, a value is set using request.setAttribute("") and in another place, that value is retrieved using request.getParameter(""). Is this right?
I know the difference between the getParameter and getAttribute. But the retrieved value intermittently becomes null.
Only those values can be retrieved using request.getAttribute which are set using request.setAttribute. And query string in GET request or request prameters in POST request can be retrieved using request.getParameter. There is no method as request.setParameter in Servlet API. Now coming to the intermittent behavior, check the URL/AJAX request on each request to server and see exactly when it has the property and its value set which you are trying to retrieve using getParameter method. Hope this clarify your issue.

How to rollback any transaction when doing test with phpUnit in symfony2

I'm testing the controllers using the crawler, but when I'm posting a form that doesn't generate any errors, it save the form in the database.
How can I prevent him to do so without changing the controller, and without testing something else.
Is there best practice about this kinds of test ?
I tried the rollback, but in the ControllerTest there is no more active transactions
You need to write your own test client class extending Symfony\Bundle\FrameworkBundle\Client.
It's because default client doesn't share connection object between requests (so you can't use transactions outside test client). If you extend test client you can handle transaction by your own.
In your client class you need make static connection object, and override method doRequest() to avoid creating new connection object every time but use our static one instead.
It's well described here:
http://alexandre-salome.fr/blog/Symfony2-Isolation-Of-Tests
When you have your own doRequest method all you need is handle transaction, so you wrap handle() method with begin and rollback. Your doRequest method could look sth like that:
protected function doRequest($request)
{
// here you need create your static connection object if it's doesn't exist yet
// and put it into service container as 'doctrine.dbal.default_connection'
(...)
self::$connection->beginTransaction();
$response = $this->kernel->handle($request);
self::$connection->rollback();
(...)
return $response
}
You can read the documentation of PHPUnit for database testing
http://www.phpunit.de/manual/3.6/en/database.html
You will need setup your database and teardown the changes you made.
If you think that the above is too complicated maybe you are interested in make a mockup of your database layer
http://www.phpunit.de/manual/3.6/en/test-doubles.html
Mockup is create a custom object based in the original object where put your own test controls. Probably in this case you are interested in mockup the Entity Manager of Doctrine

Resources