3

HTTP header sent by CasperJS contains:

...
 - User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/534.34 (KHTML, like Gecko) CasperJS/1.0.2+Phantomjs/1.8.2 Safari/534.34
...

Response does not contain SetCookie value!

When I change user agent manually:

PageSettings: {
        userAgent: "User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.97 Safari/537.22"
    }

All works fine. I'm using ASP.Net MVC.

How fix It on server side?

gor
  • 11,498
  • 5
  • 36
  • 42
Ihor Shubin
  • 10,628
  • 3
  • 25
  • 33

2 Answers2

1

This is probably because the runtime determines your browser capabilities based on the user agent header. Try to turn it off like this (or similarily) to try if it helps:

  // Global.asax.cs
  void Application_OnBeginRequest( object sender, EventArgs e )
  {
         HttpApplication app = ( HttpApplication )sender;
         HttpContext ctx = app.Context;

         ...
         if (
              ctx.Request != null &&
             !string.IsNullOrEmpty( ctx.Request.UserAgent ) &&
              ctx.Request.UserAgent.Contains( "CasperJS" )
             )
             ctx.Request.Browser.Adapters.Clear();
         ..
  }     

The snippet above is not necessarily a good idea - it totally removes the rendering adapter (which may possibly not be good!) for some browsers but it should be a good starting point for something more fancy.

I hope this helps, we were able to resolve some rendering issues with the snippet and I hope it also helps with the cookie issue.

Wiktor Zychla
  • 47,367
  • 6
  • 74
  • 106
  • As I wrote, this is a starting point. You can fine tune it according to your requirements. For example, you could try to force the support for cookies selectively for this one particular agent. – Wiktor Zychla Mar 05 '13 at 14:12
1

I found a solve here: https://stackoverflow.com/a/4816391/1010404

I put generic.browser into App_Browsers folder. File contains:

<browsers>
  <browser refID="Default">
    <capabilities>
      <capability name="cookies" value="true" />
    </capabilities>
  </browser>
</browsers>

And all work fine.

Community
  • 1
  • 1
Ihor Shubin
  • 10,628
  • 3
  • 25
  • 33
  • This could also not be a good idea as it turns of cookies unconditionally. Are you sure you want that? – Wiktor Zychla Mar 05 '13 at 14:13
  • Yes, I am. This is fixed in ASP.NET 4.5 and all browsers are assumed to support cookies, so the extra .browser file won't be needed. – Ihor Shubin Mar 05 '13 at 14:16