0

I'm trying to implement unit tests for my django rest framework project. I send protobuf files.

My code is:

def test_post(self):
    files_dir = os.path.join(settings.BASE_DIR, 'api/test_files')
    path = files_dir + '/test1_test2.bin'
    myfile = open(path,'rb')
    self.guest_client.post(
        '/config?content-disposition=%20attachment%3Bfilename%3D%22test1_test2.bin%22',
        data={'file': myfile, },
        content_type='application/octet-stream'
    )

And nothing happens. Where did I do wrong?

  • Your question does not include a minimal reproducible example making it challenging for others to help you. You don't provide sufficient information for the unfamiliar to guess the HTTP library that you're using. It seems that `guest_client` is an HTTP client attempting to POST the protobuf binary to some server but you discard the server's response. I'd expect to see `response = self.guest_client.post(...)` and use of the returned data. You say "nothing happens" which is vague. Does the server receive the request(s)? – DazWilkin Nov 05 '22 at 20:37
  • thank you for attention. I use unittest. guest_client is the instance of django.test.client. guest_client sends file to testserver. I changed code to response = self.guest_client... and tried to print response. I got nothing. How to connect to testserver? – Konstantinos Nov 06 '22 at 09:21
  • When I used json instead of protobuf everything was ok – Konstantinos Nov 06 '22 at 09:32
  • little update print(response) results Response status_code=400, "application/json" – Konstantinos Nov 06 '22 at 10:11
  • I think that's your answer. The server's expecting JSON, responding correctly when you send JSON. 400 signifies "Bad Request" when you send a proto binary. `octet-stream` is the appropriate `content-type`. I'm not familiar with Django and, without more details, I'm unable help. Good luck. – DazWilkin Nov 06 '22 at 16:35
  • Thank you anyway. If I use application/json type then I get TypeError: Object of type BufferedReader is not JSON serializable. I know that {file: myfile} is incorrect implementing but in my view I get it like file = request.data['file']. So what's the correct variant in protobuf's case? – Konstantinos Nov 06 '22 at 18:26
  • I'm not familiar with Django and so I don't know. Googling your question found this [answer](https://stackoverflow.com/a/27576436/609290) on Stack overflow which may help. Good luck! – DazWilkin Nov 06 '22 at 19:58

0 Answers0