Globally ignoring Verify.Http headers in snapshot

45 Views Asked by At

I'd like to hide some headers from the HttpResponseMessage snapshot. The documentation suggests using await Verify(response).IgnoreMember("Authorization") but I'd like to ignore it globally. I can do VerifierSettings.IgnoreMembers("Authorization") but this will ignore the property for all types not just in the headers. I can do the following for HttpRequestMessage and it works:

VerifierSettings.MemberConverter(typeof(HttpRequestMessage), "Headers", value =>
   ((Dictionary<string, object>)value)?
   .Where(k => k.Key.ToLowerInvariant() is not "authorization")
   .ToDictionary(x => x.Key, x => x.Value) is { Count: > 0 } headers ? headers : null);

This does not work for HttpResponseMessage because Verify.Http does not use a regular converter for HttpResponseMessage (it registers one but it's never called due to the `File

        VerifierSettings.RegisterFileConverter<HttpResponseMessage>(
            (instance, _) => HttpResponseSplitterResult.Convert(instance));
        VerifierSettings
            .AddExtraSettings(serializer =>
            {
                var converters = serializer.Converters;
...snip...
                // This is called and can be customized via `MemberConverter`/`IgnoreMember`/etc..
                converters.Add(new HttpRequestMessageConverter());
                // This is never called because of the `RegisterFileConverter` above
                converters.Add(new HttpResponseMessageConverter());

Is there a way to ignore specific headers for HttpResponseMessage globally without ignoring it for all types? I have several headers to ignore that have fairly common names.

0

There are 0 best solutions below