2012-10-07 1 views
1

이 테스트는 JSON이라는 게시물 본문에 데이터가 포함 된 POST API 끝점에 대한 것입니다. 전화를 걸기 전에 Content-Type'application/json'으로 설정했습니다. 그러나 형식을 테스트 할 때 isFormat('JSON') 응답이 null입니다. 내가 $request->contentType()을 덤프하면 이것은 또한 null을 생성합니다.symfony 1.4에서 sfBrowser를 사용한 기능 테스트에서 콘텐츠 유형이 전달되지 않습니다.

setHttpHeader('Content-Type','application/json')이 기능 테스트 중에 헤더를 올바르게 설정하지 않는 이유는 무엇입니까?

답변

1

귀하의 설정 방법은 정확하지만 sfBrowserBase 내부에이 버그가 :

foreach ($this->headers as $header => $value) 
{ 
    $_SERVER['HTTP_'.strtoupper(str_replace('-', '_', $header))] = $value; 
} 

접두사 HTTP와 콘텐츠 _를 설정합니다. 하지만 귀하의 행동 $ request-> getContentType() 메서드는 접두사가 없다고 가정합니다.

그래서 당신은이를 변경하는 경우 :

foreach ($this->headers as $header => $value) 
{ 
    $_SERVER[strtoupper(str_replace('-', '_', $header))] = $value; 
} 

당신이 $request->getContentType()를 제대로 만들 수 있습니다!

업데이트 here을 찾을 수 있습니다.

+0

합니다. '$ request-> getContentType()'이 HTTP없이 헤더를 찾고 있기 때문에 '$ request-> isXmlHttpRequest()'와 같이 HTTP_ 접두어가 필요하므로 ContentType에 예외를 두어야합니다. 따라서 foreach 루프는 다음과 같이됩니다 : foreach ($ this-> headers to $ header => value) {if (strtolower ($ header) == 'content-type'|| strtolower ($ header) == 'content_type') { $ _SERVER [strtoupper (str_replace ('-', '_', $ header))] = $ 값; } else {$ _SERVER [ 'HTTP_'.strtoupper (str_replace ('-', '_', $ header))] = $ value; }' –

1

@nicolx 덕분에 지금 무슨 일이 일어나고 있는지 자세히 설명하고 몇 가지 추가 지침을 제공 할 수 있습니다.

@nicolx에 의해 언급 된 바와 같이 $request->getContentType()은 HTTP_ 접두사없이 HTTP 헤더를 찾고 있습니다 (sfWebRequest의 163-173 행 참조). 그러나 sfBrowserBase는 항상 모든 머리글에 HTTP_ 접두사를 추가합니다. 그래서이 모드에서 추가 ContentType 헤더가 당신의 행동에 설정하고 감지되고 다룰 것이다

foreach($this->headers as $header => $value) 
{ 
    if(strotolower($header) == 'content-type' || strtolower($header) == 'content_type') 
    { 
    $_SERVER[strtoupper(str_replace('-','_',$header))] = $value; 
    } else { 
    $_SERVER['HTTP_'.strtoupper(str_replace('-','_',$header))] = $value; 
    } 
} 

. HTTP_ 접두사를 포함하지 않으면 다른 헤더가 작동하지 않습니다 (예 : $request->isXmlHtttpHeader()). 테스트 파일에이 메시지를 설정하더라도 실패합니다.

테스트 방법 isFormat()은 ContentType 헤더를 테스트하지 않지만 Symfony 경로 설정은 sf_format입니다. 경로를 구체적으로 설정하면 sf_format: json입니다.

some_route: 
    url: /something/to/do 
    param: {module: top, action: index, sf_format: json} 

다음 테스트

with('request')->begin()-> 
    isFormat('json')-> 
end()-> 

true를 반환합니다.

헤더 설정을 테스트하기 위해 isContentType()이라는 새 테스터 메서드를 sfTesterRequest에 추가했습니다. 이 방법에 대한 코드는 다음과 같습니다

public function isContentType($type) 
{ 
    $this->tester->is($this->request->getContentType(),$type, sprintf('request method is "%s"',strtoupper($type))); 

    return $this->getObjectToReturn(); 
} 

이 테스트는 간단하게 호출 : 실제로 다른 헤더를 나누기

with('request')->begin()-> 
    isContentType('Application/Json')-> 
end()->