How to make TempFile serializable for tests #2157
Unanswered
lucasbasquerotto
asked this question in
Questions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi, I was seeing the examples in this repository and testing them in my machine. They are very helpful and can be taken as reference for several use cases, so thanks for them!
One of the examples, tough, the
forms
example, doesn't have a test (it has a test file that is not imported and consequently not used, and its contents doesn't correspond to the main file). To make my understanding of the rocket framework deeper, I tried to create a test for it. Unfortunately I found some problems when I tried to implement.1. Convert from
T: FromForm
toForm<Contextual<T>>
and get theContext
The rocket framework provide a way to declare a parameter in a function as the type
Form<Contextual<T>>
. This parameter was used to get the context and populate the template with it. The problem is that this is done automatically by rocket, so I don't know how to create it fromT
.I saw that the result has a
values
property that is an iterator of string representing the props flattened (as what is done when serializing for the content typewww-form-urlencoded
), so it has props likeaccount.password.first
,submission.title
, etc. In the case of lists, it separates with a comma. Furthermore, its values are between[
and]
.This seems like an implementation detail of Rocket, so I was expecting a way to be able to do this with some utility function/trait using the main object (
T
), but I have found nothing. So I created a function to do that:I needed to import 2
dev-dependencies
to do that:The template expect the context
values
, so I create the context asConsidering that this seems like a rocket implementation detail, I was expecting a utility function or trait that creates
Form<Contextual<T>>
from a structT: FromForm
(and maybe using the request too, if needed), or at least theContext
, without the need of external libs. Is there a way to do it?2. Make
TempFile
implementUriDisplay<Query>
Even more important than the previous issue is that, for testing the request, I set the body with
(&data as &dyn UriDisplay<Query>).to_string()
(I took another example as a reference). This is to serialize the struct in thewww-form-urlencoded
format. For this to work, I made all structs deriveUriDisplayQuery
. Unfortunately,TempFile
does not derive it (and it isn't serializable, so I think the same problem can happen with JSON).How can I create a request in which the body is (based on) a struct with
TempFile
as one of the fields? As an alternative, how can I serialize a struct that has aTempFile
in it? I could define a raw string for the whole body (be it a JSON orwww-form-urlencoded
body), but then I would lose rust type safety. This means that I could only do type safe tests in requests in which the body is the wholeTempFile
(in which case I could define the file contents as a string), but I wouldn't be able to do that if the file is not the whole body, but part of it (like this example, in which the file is a field in a struct).The tests I created are as follows:
I added
#[cfg_attr(test, derive(UriDisplayQuery))]
to the structs of the main file, and commented thefile
field to be able to run the tests:If I uncomment the file field, I receive the error:
which is related to the issue 2.
Thanks, in advance.
Beta Was this translation helpful? Give feedback.
All reactions