Varnishtest(Varnish 5.1.3) - 未使用第二个服务器响应

时间:2018-03-20 17:06:11

标签: varnish

varnishtest "TRACE requests"

server s1 {
       rxreq
       expect req.method == "TRACE"
       expect req.url == "/1"
       txresp

       rxreq
       expect req.url == "/2"
       txresp
} -start

varnish v1 -vcl+backend {
    import std;

    sub vcl_recv {
        if(req.method == "TRACE") {
            return(synth(405, "Method Not Allowed")); 
        } 
    }

    sub vcl_deliver {
        set resp.http.x-forwarded-for = client.ip;
    }
} -start

client c1 {
    txreq -req TRACE -url "/1"
    rxresp
    expect resp.status == 405
    expect resp.reason == "Method Not Allowed"

    txreq -url "/2"
    rxresp
    expect resp.http.x-forwarded-for == "127.0.0.1"
} -run

以上是我的一个清漆测试,但是测试失败,并且从varnishtest输出看起来好像它在期望req.method ==" TRACE"声明。但是我的印象是,客户端发出的第一个调用将由第一个服务器rxreq / txresp块使用,第二个客户端调用将由第二个服务器rxreq / txresp块使用。

**   c1    0.4 === txreq -url "/2"
**** c1    0.4 txreq|GET /2 HTTP/1.1\r
**** c1    0.4 txreq|\r
***  s1    0.4 accepted fd 5 127.0.0.1 36789
**   s1    0.4 === rxreq
**** s1    0.4 rxhdr|GET /2 HTTP/1.1\r
**** s1    0.4 rxhdr|X-Forwarded-For: 127.0.0.1\r
**** s1    0.4 rxhdr|Accept-Encoding: gzip\r
**** s1    0.4 rxhdr|X-Varnish: 1003\r
**** s1    0.4 rxhdr|Host: 127.0.0.1\r
**** s1    0.4 rxhdr|\r
**** s1    0.4 rxhdrlen = 104
**** s1    0.4 http[ 0] |GET
**** s1    0.4 http[ 1] |/2
**** s1    0.4 http[ 2] |HTTP/1.1
**** s1    0.4 http[ 3] |X-Forwarded-For: 127.0.0.1
**** s1    0.4 http[ 4] |Accept-Encoding: gzip
**** s1    0.4 http[ 5] |X-Varnish: 1003
**** s1    0.4 http[ 6] |Host: 127.0.0.1
**** s1    0.4 bodylen = 0
**   s1    0.4 === expect req.method == "TRACE"
---- s1    0.4 EXPECT req.method (GET) == "TRACE" failed

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我真的不知道为什么你的上述测试不起作用,即使看起来不错。

然而我通常会这样测试它。:

varnishtest "TRACE requests"

server s1 {
       rxreq
       txresp
       rxreq
       txresp

} -start

varnish v1 -vcl+backend {
    import std;

    sub vcl_recv {
        if(req.method == "TRACE") {
            return(synth(405, "Method Not Allowed")); 
        } 
    }

    sub vcl_deliver {
        set resp.http.x-forwarded-for = client.ip;
    }
} -start

client c1 {
    txreq -req TRACE -url "/1"
    rxresp
    expect resp.status == 405
    expect resp.reason == "Method Not Allowed"
    expect resp.http.x-forward-for == <undef>


} -run

client c2 {
    txreq -url "/2"
    rxresp
    expect resp.status == 200
    expect resp.http.x-forwarded-for == "127.0.0.1"
} -run

这传递,并且“仅”验证响应,因为请求无论如何是手工制作的,因此输入expect并不是真正的新的。

还将它分成多个客户端,这是一个很好的做法我认为 - 因为单元测试应该只做一件事。