1
1
using System ;
2
2
using System . Collections . Generic ;
3
- using System . IO ;
4
3
using System . Linq ;
5
4
using System . Net . Http ;
6
- using System . Reflection ;
7
- using System . Runtime . Serialization ;
8
5
using MessagePack ;
9
6
using SecTester . Repeater . Internal ;
10
7
using SecTester . Repeater . Runners ;
@@ -20,75 +17,66 @@ public record IncomingRequest(Uri Url) : IRequest
20
17
private const string BodyKey = "body" ;
21
18
private const string ProtocolKey = "protocol" ;
22
19
23
- private static readonly Dictionary < string , Protocol > ProtocolEntries = typeof ( Protocol )
24
- . GetFields ( BindingFlags . Public | BindingFlags . Static )
25
- . Select ( field => new
26
- {
27
- Value = ( Protocol ) field . GetValue ( null ) ,
28
- StringValue = field . GetCustomAttribute < EnumMemberAttribute > ( ) ? . Value ?? field . Name
29
- } )
30
- . ToDictionary ( x => MessagePackNamingPolicy . SnakeCase . ConvertName ( x . StringValue ) , x => x . Value ) ;
31
-
32
- [ Key ( ProtocolKey ) ]
33
- public Protocol Protocol { get ; set ; } = Protocol . Http ;
20
+ [ Key ( ProtocolKey ) ] public Protocol Protocol { get ; set ; } = Protocol . Http ;
34
21
35
- private IEnumerable < KeyValuePair < string , IEnumerable < string > > > _headers = Enumerable . Empty < KeyValuePair < string , IEnumerable < string > > > ( ) ;
36
-
37
- [ Key ( HeadersKey ) ]
38
- public IEnumerable < KeyValuePair < string , IEnumerable < string > > > Headers
39
- {
40
- get => _headers ;
41
- // ADHOC: convert from a kind of assignable type to formatter resolvable type
42
- set => _headers = value . AsEnumerable ( ) ;
43
- }
22
+ [ Key ( HeadersKey ) ] public IEnumerable < KeyValuePair < string , IEnumerable < string > > > Headers { get ; set ; } = new Dictionary < string , IEnumerable < string > > ( ) ;
44
23
45
- [ Key ( BodyKey ) ]
46
- public string ? Body { get ; set ; }
24
+ [ Key ( BodyKey ) ] public string ? Body { get ; set ; }
47
25
48
- [ Key ( MethodKey ) ]
49
- public HttpMethod Method { get ; set ; } = HttpMethod . Get ;
26
+ [ Key ( MethodKey ) ] public HttpMethod Method { get ; set ; } = HttpMethod . Get ;
50
27
51
- [ Key ( UrlKey ) ]
52
- public Uri Url { get ; set ; } = Url ?? throw new ArgumentNullException ( nameof ( Url ) ) ;
28
+ [ Key ( UrlKey ) ] public Uri Url { get ; set ; } = Url ?? throw new ArgumentNullException ( nameof ( Url ) ) ;
53
29
54
30
public static IncomingRequest FromDictionary ( Dictionary < object , object > dictionary )
55
31
{
56
- var protocol = ! dictionary . ContainsKey ( ProtocolKey ) || ( dictionary . TryGetValue ( ProtocolKey , out var p1 ) && p1 is null )
57
- ? Protocol . Http
58
- : dictionary . TryGetValue ( ProtocolKey , out var p2 ) && p2 is string && ProtocolEntries . TryGetValue ( p2 . ToString ( ) , out var e )
59
- ? e
60
- : throw new InvalidDataException ( FormatPropertyError ( ProtocolKey ) ) ;
61
-
62
- var uri = dictionary . TryGetValue ( UrlKey , out var u ) && u is string
63
- ? new Uri ( u . ToString ( ) )
64
- : throw new InvalidDataException ( FormatPropertyError ( UrlKey ) ) ;
65
-
66
- var method = dictionary . TryGetValue ( MethodKey , out var m ) && m is string
67
- ? new HttpMethod ( m . ToString ( ) )
68
- : HttpMethod . Get ;
32
+ var protocol = GetProtocolFromDictionary ( dictionary ) ;
33
+ var headers = GetHeadersFromDictionary ( dictionary ) ;
34
+ var body = GetBodyFromDictionary ( dictionary ) ;
35
+ var method = GetMethodFromDictionary ( dictionary ) ;
36
+ var url = GetUrlFromDictionary ( dictionary ) ;
69
37
70
- var body = dictionary . TryGetValue ( BodyKey , out var b ) && b is string ? b . ToString ( ) : null ;
71
-
72
- var headers = dictionary . TryGetValue ( HeadersKey , out var h ) && h is Dictionary < object , object > value
73
- ? MapHeaders ( value )
74
- : Enumerable . Empty < KeyValuePair < string , IEnumerable < string > > > ( ) ;
75
-
76
- return new IncomingRequest ( uri )
38
+ return new IncomingRequest ( url ! )
77
39
{
78
40
Protocol = protocol ,
41
+ Headers = headers ,
79
42
Body = body ,
80
- Method = method ,
81
- Headers = headers
43
+ Method = method
82
44
} ;
83
45
}
84
46
85
- private static IEnumerable < KeyValuePair < string , IEnumerable < string > > > MapHeaders ( Dictionary < object , object > headers ) =>
86
- headers . Select ( kvp => kvp . Value switch
87
- {
88
- IEnumerable < object > strings => new KeyValuePair < string , IEnumerable < string > > ( kvp . Key . ToString ( ) , strings . Select ( x => x . ToString ( ) ) ) ,
89
- null => new KeyValuePair < string , IEnumerable < string > > ( kvp . Key . ToString ( ) , Enumerable . Empty < string > ( ) ) ,
90
- _ => new KeyValuePair < string , IEnumerable < string > > ( kvp . Key . ToString ( ) , new [ ] { kvp . Value . ToString ( ) } )
91
- } ) ;
47
+ private static Protocol GetProtocolFromDictionary ( Dictionary < object , object > dictionary ) =>
48
+ dictionary . TryGetValue ( ProtocolKey , out var protocolObj ) && protocolObj is string protocolStr
49
+ ? ( Protocol ) Enum . Parse ( typeof ( Protocol ) , protocolStr , true )
50
+ : Protocol . Http ;
51
+
52
+ private static IEnumerable < KeyValuePair < string , IEnumerable < string > > > GetHeadersFromDictionary ( Dictionary < object , object > dictionary ) =>
53
+ dictionary . TryGetValue ( HeadersKey , out var headersObj ) && headersObj is Dictionary < object , object > headersDict
54
+ ? ConvertToHeaders ( headersDict )
55
+ : new Dictionary < string , IEnumerable < string > > ( ) ;
56
+
57
+ private static string ? GetBodyFromDictionary ( Dictionary < object , object > dictionary ) =>
58
+ dictionary . TryGetValue ( BodyKey , out var bodyObj ) ? bodyObj ? . ToString ( ) : null ;
59
+
60
+ private static HttpMethod GetMethodFromDictionary ( Dictionary < object , object > dictionary ) =>
61
+ dictionary . TryGetValue ( MethodKey , out var methodObj ) && methodObj is string methodStr
62
+ ? HttpMethods . Items . TryGetValue ( methodStr , out var m ) && m is not null
63
+ ? m
64
+ : HttpMethod . Get
65
+ : HttpMethod . Get ;
92
66
93
- private static string FormatPropertyError ( string propName ) => $ "{ propName } is either null or has an invalid data type or value";
67
+ private static Uri ? GetUrlFromDictionary ( Dictionary < object , object > dictionary ) =>
68
+ dictionary . TryGetValue ( UrlKey , out var urlObj ) && urlObj is string urlStr
69
+ ? new Uri ( urlStr )
70
+ : null ;
71
+
72
+ private static IEnumerable < KeyValuePair < string , IEnumerable < string > > > ConvertToHeaders ( Dictionary < object , object > headers ) =>
73
+ headers . ToDictionary (
74
+ kvp => kvp . Key . ToString ( ) ! ,
75
+ kvp => kvp . Value switch
76
+ {
77
+ IEnumerable < object > list => list . Select ( v => v . ToString ( ) ! ) ,
78
+ string str => new [ ] { str } ,
79
+ _ => Enumerable . Empty < string > ( )
80
+ }
81
+ ) ;
94
82
}
0 commit comments