@@ -4,6 +4,8 @@ use hyper::Body;
4
4
use serde:: Serialize ;
5
5
use std:: str:: FromStr ;
6
6
7
+ const USER_AGENT : & str = concat ! ( "aws-lambda-rust/" , env!( "CARGO_PKG_VERSION" ) ) ;
8
+
7
9
pub ( crate ) trait IntoRequest {
8
10
fn into_req ( self ) -> Result < Request < Body > , Error > ;
9
11
}
@@ -20,6 +22,7 @@ impl IntoRequest for NextEventRequest {
20
22
fn into_req ( self ) -> Result < Request < Body > , Error > {
21
23
let req = Request :: builder ( )
22
24
. method ( Method :: GET )
25
+ . header ( "User-Agent" , USER_AGENT )
23
26
. uri ( Uri :: from_static ( "/2018-06-01/runtime/invocation/next" ) )
24
27
. body ( Body :: empty ( ) ) ?;
25
28
Ok ( req)
@@ -57,6 +60,10 @@ fn test_next_event_request() {
57
60
let req = req. into_req ( ) . unwrap ( ) ;
58
61
assert_eq ! ( req. method( ) , Method :: GET ) ;
59
62
assert_eq ! ( req. uri( ) , & Uri :: from_static( "/2018-06-01/runtime/invocation/next" ) ) ;
63
+ assert ! ( match req. headers( ) . get( "User-Agent" ) {
64
+ Some ( header) => header. to_str( ) . unwrap( ) . starts_with( "aws-lambda-rust/" ) ,
65
+ None => false ,
66
+ } ) ;
60
67
}
61
68
62
69
// /runtime/invocation/{AwsRequestId}/response
75
82
let body = serde_json:: to_vec ( & self . body ) ?;
76
83
let body = Body :: from ( body) ;
77
84
78
- let req = Request :: builder ( ) . method ( Method :: POST ) . uri ( uri) . body ( body) ?;
85
+ let req = Request :: builder ( )
86
+ . header ( "User-Agent" , USER_AGENT )
87
+ . method ( Method :: POST )
88
+ . uri ( uri)
89
+ . body ( body) ?;
79
90
Ok ( req)
80
91
}
81
92
}
@@ -90,6 +101,10 @@ fn test_event_completion_request() {
90
101
let expected = Uri :: from_static ( "/2018-06-01/runtime/invocation/id/response" ) ;
91
102
assert_eq ! ( req. method( ) , Method :: POST ) ;
92
103
assert_eq ! ( req. uri( ) , & expected) ;
104
+ assert ! ( match req. headers( ) . get( "User-Agent" ) {
105
+ Some ( header) => header. to_str( ) . unwrap( ) . starts_with( "aws-lambda-rust/" ) ,
106
+ None => false ,
107
+ } ) ;
93
108
}
94
109
95
110
// /runtime/invocation/{AwsRequestId}/error
@@ -108,6 +123,7 @@ impl<'a> IntoRequest for EventErrorRequest<'a> {
108
123
let req = Request :: builder ( )
109
124
. method ( Method :: POST )
110
125
. uri ( uri)
126
+ . header ( "User-Agent" , USER_AGENT )
111
127
. header ( "lambda-runtime-function-error-type" , "unhandled" )
112
128
. body ( body) ?;
113
129
Ok ( req)
@@ -127,6 +143,10 @@ fn test_event_error_request() {
127
143
let expected = Uri :: from_static ( "/2018-06-01/runtime/invocation/id/error" ) ;
128
144
assert_eq ! ( req. method( ) , Method :: POST ) ;
129
145
assert_eq ! ( req. uri( ) , & expected) ;
146
+ assert ! ( match req. headers( ) . get( "User-Agent" ) {
147
+ Some ( header) => header. to_str( ) . unwrap( ) . starts_with( "aws-lambda-rust/" ) ,
148
+ None => false ,
149
+ } ) ;
130
150
}
131
151
132
152
// /runtime/init/error
@@ -140,6 +160,7 @@ impl IntoRequest for InitErrorRequest {
140
160
let req = Request :: builder ( )
141
161
. method ( Method :: POST )
142
162
. uri ( uri)
163
+ . header ( "User-Agent" , USER_AGENT )
143
164
. header ( "lambda-runtime-function-error-type" , "unhandled" )
144
165
. body ( Body :: empty ( ) ) ?;
145
166
Ok ( req)
@@ -153,4 +174,8 @@ fn test_init_error_request() {
153
174
let expected = Uri :: from_static ( "/2018-06-01/runtime/init/error" ) ;
154
175
assert_eq ! ( req. method( ) , Method :: POST ) ;
155
176
assert_eq ! ( req. uri( ) , & expected) ;
177
+ assert ! ( match req. headers( ) . get( "User-Agent" ) {
178
+ Some ( header) => header. to_str( ) . unwrap( ) . starts_with( "aws-lambda-rust/" ) ,
179
+ None => false ,
180
+ } ) ;
156
181
}
0 commit comments