蝗虫传递header信息

31

有没有可能让Locust传递一个带有安全令牌的头部命令来对API进行负载测试?

我正在尝试为编码器测试我们的API,其中包含一个带有令牌的标头标志,因为被测试的服务器必须接收请求中的令牌,即:

curl -H "Authorization: Token token string" http://someserver


3
你目前是如何执行这项任务的?self.client.get(uri, headers={"Authorization": "Token token string"})应该可行。 - enderland
我会尝试并回报,谢谢。 - Liam Douglas
如果使用基本身份验证呢?@enderland - uberrebu
1
@uberrebu,你应该使用 auth 字段,例如 auth=("username", "password"),并将 headers 字段保持为 None - Matt Doyle
在四处搜索后,我最终找到了它...是的,正确的...谢谢! - uberrebu
3个回答

45

是的,您可以使用:

token_string = "token string"

resp = self.client.post(
            url="http://someserver",
            data=json.dumps(data),
            auth=None,
            headers={"authorization": "Token " + token_string},
            name="http://someserver",
        )

27

此外,如果您希望为每个请求使用相同的标题,您还可以在on_start方法中将它们设置给客户端。这些标题将自动用于每个客户端请求。

class User(HttpUser):

    def on_start(self):
        self.client.headers = {'Authorization': 'my-auth-token'}

    @task
    def my_authenticated_task(self):
        self.client.post('enspoint')  # this will use headers we set earlier

1
你如何在 FastHttpUser 上实现这个? - Shinto C V

1
class User(HttpUser):

    host = 'http://127.0.0.1:8000'      
    @task
    def predict_post(self):
        self.client.post("/route/", data=json.dumps({
        "text": "finish all the track to hear and great good tune u do a great job alan walker",
        "max_op_words": 10,
        "max_time": 1,
        "max_predictions": 1
        }),auth=None,
        headers={"authtoken":"your-auth-token", 'content-type': 'application/json'})

我缺少内容类型标头,因此出现401错误授权错误。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接