Lucas_Cavalcanti 19 de dez. de 2012
o que você quer dizer com o conteúdo do result?
o html gerado?
danilomunoz 19 de dez. de 2012
Isso mesmo!
Quero acessar o que defini através de result.use(http()).body()
Valeu,
Lucas_Cavalcanti 19 de dez. de 2012
nesse caso dá pra fazer um cara bem parecido com o MockSerialization mesmo…
se fosse algo no JSP é que seria meio difícil de fazer.
não lembro de ter visto esse mock, mas se quiser ajuda pra implementar dá um toque, e se quiser fazer um pull request seria muito bem vindo =)
danilomunoz 20 de dez. de 2012
Então … eu comecei a fazer aqui mas não deu muito certo. Agora que você se propôs a dar um help vou tentar novamente…
Sorry, eu não entendi o que você disse aqui
Lucas Cavalcanti:
não lembro de ter visto esse mock, mas se quiser ajuda pra implementar dá um toque, e se quiser fazer um pull request seria muito bem vindo =)
Legal!!
danilomunoz 20 de dez. de 2012
Lucas,
Você recomenda levar essa discussão para uma PullRequest ou deixar por aqui mesmo?
Eu pensei em fazer no meu mock:
result = new MockSerializationResult ();
myController = new MyController ( result );
...
myController . doStuff ();
String json = result . serializedResult ();
MyController#doStuff:
@Get
@Path ( "doStuff" )
public void getData ( String itemName ) {
...
result . use ( Results . http ()). addHeader ( "Content-Type" , "application/json" ). body ( json );
}
Porém, debugando a classe MockSerialization, percebi que o response.getContentType() não foi setado ao fazer addHeader(“Content-Type”, “application/json”). Tenho a impressão que as responses ou o tempo que elas são usadas não são o mesmo:
/**
* Retrieve the string with the serialized ( JSON / XML ) Object if have one as response .
*
* @ return String with the object serialized
*/
public String serializedResult () throws Exception {
if ( "application/xml" . equals ( response . getContentType ())){
return response . getContentAsString ();
}
if ( "application/json" . equals ( response . getContentType ())){
return response . getContentAsString ();
}
return null ;
}
Obrigado,
Lucas_Cavalcanti 20 de dez. de 2012
não dá pra usar o mockSerializationResult, pq ele só funciona com results que são Serialization (json(), xml() e representation())
precisa criar um mock result inspirado nele…
/***
* Copyright (c) 2009 Caelum - www.caelum.com.br/opensource
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package br.com.caelum.vraptor.util.test ;
This file has been truncated. show original
seria algo como mudar o instanceView para instanciar ou mockar o HttpResult, acumulando o body e os headers.
use o mockResponse e passe pra esse cara:
/***
* Copyright (c) 2009 Caelum - www.caelum.com.br/opensource
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
*
*/
package br.com.caelum.vraptor.view ;
This file has been truncated. show original
deve funcionar.
danilomunoz 20 de dez. de 2012
Implementado … não sei se está muito simplista, mas resolveu meu caso de testes aqui
Também não sabia como iniciar direito o DefaultStatus … mas passei uns null e funcionou.
Veja se está legal:
public class MockResultHttp extends MockResult {
private MockHttpServletResponse response = new MockHttpServletResponse ();
public MockResultHttp () {
super ( new CglibProxifier ( new ObjenesisInstanceCreator ()));
}
@Override
public < T extends View > T use ( Class < T > view ) {
if ( view . isAssignableFrom ( HttpResult . class )){
DefaultHttpResult defaultHttpResult = new DefaultHttpResult ( response , new DefaultStatus ( response , this , null , proxifier , null ));
return view . cast ( defaultHttpResult );
}
return proxifier . proxify ( view , returnOnFinalMethods ( view ));
}
public String getBody (){
response . getWriter (). flush ();
return response . getContentAsString ();
}
}
Lucas_Cavalcanti 20 de dez. de 2012
parece legal!
talvez adicionar um método pra ver os headers/content-type tb =)
manda um pull request com isso, plz?
danilomunoz 20 de dez. de 2012
Já está lá:
https://github.com/caelum/vraptor/pull/484
Só falta verificar se vai deixar com este nome a classe mesmo.
Lucas Cavalcanti:
parece legal!
talvez adicionar um método pra ver os headers/content-type tb =)
manda um pull request com isso, plz?
Lucas_Cavalcanti 20 de dez. de 2012
fiz uns comentários lá =)