How to add test-case of a route only with pathEnd - spray

I have a route
get {
pathEnd {
respondWithMediaType(`text/html`) { // XML is marshalled to `text/xml` by default, so we simply override here
complete("[]")
}
}
}
I am trying to add a test-case for this route like this
Get() ~> route ~> check {
mediaType === MediaTypes.`text/html`
responseAs[String] === "[]"
}
But I am getting
[error] Request was not handled
Can anyone tell me how to write test-case for this route? Thanks in advance.

Related

Grails 2.3.8 check url mappings for string occurence

I am trying to map the following
//localhost:8080/user/login/&debug=1
...
//localhost:8080/user/&debug=1
If there is any occurrence of string '&debug=1' then execute some-controller's action.
You can use a filter to do the redirect when required.
class DebugFilters {
def filters = {
debugCheck(controller: '*', action: '*') {
before = {
if (params.debug == '1') {
redirect(controller: 'some', action: 'debug')
return false
}
}
}
}
}
If you want to just switch between controllers and actions for a particular url mapping then you can also use a url mapping as below, instead of the filter altogether:
//UrlMappings.groovy
"/user/login" {
controller = { params.debug == '1' ? 'some' : 'user' }
action = { params.debug == '1' ? 'debug': 'index' }
// Note the use of a closure in ternary operations
// params is available in a closure (delegated)
// because it is not available in by default
}
You could use a URLMapping like this
"/**&debug=1"(controller:"defaultDebug"){
action = [GET: "show"]
}
By using the double wildcard you can catch anything that ends with &debug=1

How to Rewrite hardcoded url in yii?

How to Rewrite hardcoded url in yii?
I have made a url as follows,
CHtml::link($visit->health_institute_name, array('hospitals_single', 'id'=>$visit->health_institute_id));
It redireceted to the url as follow,
http://abc.com/hospital?name=yyy&id=14#ad-image-0
I Just want the url to be as follows,
http://abc.com/yyy
ie: i need to remove hospital?name= and &id=14#ad-image-0 from the url...
Can any one help?
In your urlManager rules, add this after all other rules:
'<name>' => 'hospital/view',
assuming view is the action you want to call - replace it with your action name
Then your link as follows:
CHtml::link($visit->health_institute_name, Yii::app()->getBaseUrl(true).'/'.$visit->name);
if you want to make a user friendly and seo-friendly like http://abc.com/hospital/yyy
You can use this:
class CarUrlRule extends CBaseUrlRule
{
public $connectionID = 'db';
public function createUrl($manager,$route,$params,$ampersand)
{
if ($route==='car/index')
{
if (isset($params['manufacturer'], $params['model']))
return $params['manufacturer'] . '/' . $params['model'];
else if (isset($params['manufacturer']))
return $params['manufacturer'];
}
return false; // this rule does not apply
}
public function parseUrl($manager,$request,$pathInfo,$rawPathInfo)
{
if (preg_match('%^(\w+)(/(\w+))?$%', $pathInfo, $matches))
{
// check $matches[1] and $matches[3] to see
// if they match a manufacturer and a model in the database
// If so, set $_GET['manufacturer'] and/or $_GET['model']
// and return 'car/index'
}
return false; // this rule does not apply
}
}
More informatino there !
http://www.yiiframework.com/doc/guide/1.1/fr/topics.url#using-custom-url-rule-classes

Does Grails Filter `actionExclude` work with method-based routing?

I have a method-based route like:
name base: "/" {
controller="api"
action=[GET: "welcome", POST: "post"]
}
And I'd like to apply a filter to handle authorization, e.g.
class RequestFilters {
def filters = {
authorizeRequest(controller: 'api', actionExclude: 'welcome') {
before = {
log.debug("Applying authorization filter.")
}
}
}
}
But when I apply this in practice, the filter runs on all requests (even GET requests, which should use the welcome method and thus should not trigger this filter.)
When I inspect the code running the in the filter, I see that params.action is set to the Map from my routing file, rather than to "welcome". Not sure if this is related to the issue.
My current workaround (which feels very wrong) is to add the following to my filter's body:
if(params.action[request.method] == 'welcome'){
return true
}
The short question is: does Grails support this combination of method-based routing + action-name-based filtering? If so, how? If not, what are some reasonable alternatives for restructuring this logic?
Thanks!
You need to use the filter as below:
class RequestFilters {
def filters = {
authorizeRequest(controller:'api', action:'*', actionExclude:'welcome'){
before = {
log.debug("Applying authorization filter.")
return true
}
}
}
}
Apply the filter to all actions of the controller but "welcome". :)
If there is no other welcome action in the other controllers then, you would not need the controller specified in the filter as well.
authorizeRequest(action:'*', actionExclude:'welcome'){...}

grails url mapping for google ajax search

This is probably a very simple problem but for the life of me I can't get it to work.
I need to redirect google requests for ajax generated code to return a html template for indexing
I have the following in my urlmappings.conf
"/?_escaped_fragment_=$id"(controller:"google",action:"getOfferDetails")
However if I enter mysite?_escaped_fragment_=200 in the browser the controller is not called
If however I enter mysite_escaped_fragment=200 the controller is called and the action executed.
Any suggestions would be greatly appreciated.
Thanks
Ash
You can not use '?' char in the route matching i.e. it will be ignored.
Use this filter instead (put this class in the config folder w/ fileName CrawlerFilters.groovy):
class CrawlerFilters {
def filters = {
google(controller: '*', action: '*') {
before = {
boolean isCrawler = webRequest.params._escaped_fragment_ != null
if (isCrawler && !request._alreadyForwarded) {
request._alreadyForwarded = true
forward controller: 'google', action: 'getOfferDetails'
}
}
}
}`

Setting up the filter without controllers

I have a running tomcat at localost.
I wish to write a grails filter such that when ever user goes to localhost/filter/ intercept the call and do some processing. I created a filter which is inside conf folder
class TestFilters {
def filters = {
filter(uri:'/filter') {
before = {
}
after = {
}
afterView = {
}
}
}
}
after setting this up when I go to localhost/filter/ I get only 404 error.
Where I'm making the mistake?
Thanks in advance
If you have no FilterController the url localhost/filter has no ressource to show - so you get a 404 error. You have to adapt your UrlMappings so that localhost/filter is a valid url of application.
Add the following to UrlMappings.groovy:
"/filter" (controller: "yourController", action:"yourAction")
Now - the url localhost/filter points to yourAction in yourController and the filter should be applied.

Resources