我定义了一个接受文件的API端点(例如使用Django REST Framework)。 在Django中,可以在检查响应时使用内容处置标头。
现在,如果我们想在测试端点时设置标头,如何使用REST-Framework的APITestCase包含此标头?
到目前为止我尝试的是,但它似乎不接受标题。
for (int i = 0; i < tabLayout.getTabCount(); i++) {
TabLayout.Tab tab = tabLayout.getTabAt(i);
if (tab != null) {
TextView tabTextView = new TextView(this);
tab.setCustomView(tabTextView);
tabTextView.getLayoutParams().width = ViewGroup.LayoutParams.WRAP_CONTENT;
tabTextView.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
tabTextView.setText(tab.getText());
if (i == 0) {
tabTextView.setTextSize(16);
}
}
}
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
ViewGroup vg = (ViewGroup) tabLayout.getChildAt(0);
ViewGroup vgTab = (ViewGroup) vg.getChildAt(tab.getPosition());
int tabChildsCount = vgTab.getChildCount();
for (int i = 0; i < tabChildsCount; i++) {
View tabViewChild = vgTab.getChildAt(i);
if (tabViewChild instanceof TextView) {
((TextView) tabViewChild).setTextSize(16);
}
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
ViewGroup vg = (ViewGroup) tabLayout.getChildAt(0);
ViewGroup vgTab = (ViewGroup) vg.getChildAt(tab.getPosition());
int tabChildsCount = vgTab.getChildCount();
for (int i = 0; i < tabChildsCount; i++) {
View tabViewChild = vgTab.getChildAt(i);
if (tabViewChild instanceof TextView) {
((TextView) tabViewChild).setTextSize(14);
}
}
}
}
答案 0 :(得分:11)
找到了答案!
Django在其FileUploadParser中为此标头提供了固定关键字。它是:class TestSaleViews(APITestCase):
def test_sale_detail_view(self):
f = create_named_temporary_file()
files = {'archive': f}
basename = os.path.basename(f.name)
headers = {
'content-disposition': 'attachment; filename={}'.format(basename),
}
response = self.client.post(url, files, format='multipart', **headers)
所以我需要更换它瞧瞧:工作!
HTTP_CONTENT_DISPOSITION
https://github.com/encode/django-rest-framework/blob/master/rest_framework/parsers.py#L206