harn-vm 0.10.50

Async bytecode virtual machine for the Harn programming language
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
use crate::value::VmDictExt;
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::sync::Mutex;

use crate::value::VmValue;

#[derive(Clone, Debug)]
pub(super) struct MockResponse {
    pub(super) status: i64,
    pub(super) body: String,
    pub(super) headers: crate::value::DictMap,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct HttpMockResponse {
    pub status: i64,
    pub body: String,
    pub headers: BTreeMap<String, String>,
}

impl HttpMockResponse {
    pub fn new(status: i64, body: impl Into<String>) -> Self {
        Self {
            status,
            body: body.into(),
            headers: BTreeMap::new(),
        }
    }

    pub fn with_header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
        self.headers.insert(name.into(), value.into());
        self
    }
}

impl From<HttpMockResponse> for MockResponse {
    fn from(value: HttpMockResponse) -> Self {
        Self {
            status: value.status,
            body: value.body,
            headers: value
                .headers
                .into_iter()
                .map(|(key, value)| {
                    (
                        crate::value::intern_key(&key),
                        VmValue::String(arcstr::ArcStr::from(value)),
                    )
                })
                .collect(),
        }
    }
}

#[derive(Debug)]
struct HttpMock {
    method: String,
    url_pattern: String,
    responses: Vec<MockResponse>,
    next_response: usize,
}

#[derive(Clone, Debug)]
struct HttpMockCall {
    method: String,
    url: String,
    headers: crate::value::DictMap,
    body: Option<String>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct HttpMockCallSnapshot {
    pub method: String,
    pub url: String,
    pub headers: BTreeMap<String, String>,
    pub body: Option<String>,
}

thread_local! {
    static HTTP_MOCKS: RefCell<Vec<HttpMock>> = const { RefCell::new(Vec::new()) };
    static HTTP_MOCK_CALLS: RefCell<Vec<HttpMockCall>> = const { RefCell::new(Vec::new()) };
}

/// HTTP fixture state bound to one typed `Harness`.
///
/// The legacy ambient mock registry is thread-local. A typed harness can cross
/// executor threads between fixture registration and an awaited request, so
/// its deterministic transport state must travel with the harness instead.
#[derive(Debug, Default)]
pub(crate) struct HttpMockRegistry {
    inner: Mutex<HttpMockRegistryInner>,
}

#[derive(Debug, Default)]
struct HttpMockRegistryInner {
    mocks: Vec<HttpMock>,
    calls: Vec<HttpMockCall>,
}

impl HttpMockRegistry {
    pub(crate) fn clear(&self) {
        let mut inner = self.inner.lock().expect("harness HTTP mocks poisoned");
        inner.mocks.clear();
        inner.calls.clear();
    }

    pub(crate) fn has_match(&self, method: &str, url: &str) -> bool {
        self.inner
            .lock()
            .expect("harness HTTP mocks poisoned")
            .mocks
            .iter()
            .any(|mock| {
                !mock.responses.is_empty()
                    && (mock.method == "*" || mock.method.eq_ignore_ascii_case(method))
                    && url_matches(&mock.url_pattern, url)
            })
    }

    pub(super) fn register(
        &self,
        method: impl Into<String>,
        url_pattern: impl Into<String>,
        responses: Vec<MockResponse>,
    ) {
        let method = method.into();
        let url_pattern = url_pattern.into();
        let mut inner = self.inner.lock().expect("harness HTTP mocks poisoned");
        inner
            .mocks
            .retain(|mock| !(mock.method == method && mock.url_pattern == url_pattern));
        inner.mocks.push(HttpMock {
            method,
            url_pattern,
            responses,
            next_response: 0,
        });
    }

    pub(super) fn consume(
        &self,
        method: &str,
        url: &str,
        headers: crate::value::DictMap,
        body: Option<String>,
    ) -> Option<MockResponse> {
        let mut inner = self.inner.lock().expect("harness HTTP mocks poisoned");
        let response = inner.mocks.iter_mut().find_map(|mock| {
            if (mock.method == "*" || mock.method.eq_ignore_ascii_case(method))
                && url_matches(&mock.url_pattern, url)
            {
                let last_index = mock.responses.len().checked_sub(1)?;
                let index = mock.next_response.min(last_index);
                let response = mock.responses[index].clone();
                if mock.next_response < last_index {
                    mock.next_response += 1;
                }
                Some(response)
            } else {
                None
            }
        })?;
        inner.calls.push(HttpMockCall {
            method: method.to_string(),
            url: url.to_string(),
            headers,
            body,
        });
        Some(response)
    }

    pub(crate) fn calls_value(&self, redact_sensitive: bool) -> Vec<VmValue> {
        let inner = self.inner.lock().expect("harness HTTP mocks poisoned");
        http_mock_calls_from(&inner.calls, redact_sensitive)
    }
}

pub(super) fn reset_http_mocks() {
    HTTP_MOCKS.with(|mocks| mocks.borrow_mut().clear());
    HTTP_MOCK_CALLS.with(|calls| calls.borrow_mut().clear());
}

pub(super) fn clear_http_mocks() {
    reset_http_mocks();
}

pub fn push_http_mock(
    method: impl Into<String>,
    url_pattern: impl Into<String>,
    responses: Vec<HttpMockResponse>,
) {
    let responses = if responses.is_empty() {
        vec![MockResponse::from(HttpMockResponse::new(200, ""))]
    } else {
        responses.into_iter().map(MockResponse::from).collect()
    };
    register_http_mock(method.into(), url_pattern.into(), responses);
}

pub(super) fn register_http_mock(
    method: impl Into<String>,
    url_pattern: impl Into<String>,
    responses: Vec<MockResponse>,
) {
    let method = method.into();
    let url_pattern = url_pattern.into();
    HTTP_MOCKS.with(|mocks| {
        let mut mocks = mocks.borrow_mut();
        // Re-registering the same (method, url_pattern) replaces the prior
        // mock so tests can override per-case responses without first calling
        // http_mock_clear(). Without this, the original mock keeps matching
        // forever and the new one is dead.
        mocks.retain(|mock| !(mock.method == method && mock.url_pattern == url_pattern));
        mocks.push(HttpMock {
            method,
            url_pattern,
            responses,
            next_response: 0,
        });
    });
}

pub fn http_mock_calls_snapshot() -> Vec<HttpMockCallSnapshot> {
    HTTP_MOCK_CALLS.with(|calls| {
        calls
            .borrow()
            .iter()
            .map(|call| HttpMockCallSnapshot {
                method: call.method.clone(),
                url: call.url.clone(),
                headers: call
                    .headers
                    .iter()
                    .map(|(key, value)| (key.to_string(), value.display()))
                    .collect(),
                body: call.body.clone(),
            })
            .collect()
    })
}

pub(super) fn http_mock_calls_value(redact_sensitive: bool) -> Vec<VmValue> {
    HTTP_MOCK_CALLS.with(|calls| http_mock_calls_from(&calls.borrow(), redact_sensitive))
}

fn http_mock_calls_from(calls: &[HttpMockCall], redact_sensitive: bool) -> Vec<VmValue> {
    calls
        .iter()
        .map(|call| {
            let mut dict = BTreeMap::new();
            dict.put_str("method", call.method.as_str());
            dict.put_str("url", redact_mock_call_url(&call.url, redact_sensitive));
            dict.insert(
                "headers".to_string(),
                VmValue::dict(mock_call_headers_value(&call.headers, redact_sensitive)),
            );
            dict.insert(
                "body".to_string(),
                match &call.body {
                    Some(body) => VmValue::String(arcstr::ArcStr::from(body.as_str())),
                    None => VmValue::Nil,
                },
            );
            VmValue::dict(dict)
        })
        .collect()
}

pub(super) fn parse_mock_responses(response: &crate::value::DictMap) -> Vec<MockResponse> {
    let scripted = response
        .get("responses")
        .and_then(|value| match value {
            VmValue::List(items) => Some(
                items
                    .iter()
                    .filter_map(|item| item.as_dict().map(parse_mock_response_dict))
                    .collect::<Vec<_>>(),
            ),
            _ => None,
        })
        .unwrap_or_default();

    if scripted.is_empty() {
        vec![parse_mock_response_dict(response)]
    } else {
        scripted
    }
}

fn parse_mock_response_dict(response: &crate::value::DictMap) -> MockResponse {
    let status = response
        .get("status")
        .and_then(|v| v.as_int())
        .unwrap_or(200);
    let body = response
        .get("body")
        .map(|v| v.display())
        .unwrap_or_default();
    let headers = response
        .get("headers")
        .and_then(|v| v.as_dict())
        .cloned()
        .unwrap_or_default();
    MockResponse {
        status,
        body,
        headers,
    }
}

pub(super) fn consume_http_mock(
    method: &str,
    url: &str,
    headers: crate::value::DictMap,
    body: Option<String>,
) -> Option<MockResponse> {
    let response = HTTP_MOCKS.with(|mocks| {
        let mut mocks = mocks.borrow_mut();
        for mock in mocks.iter_mut() {
            if (mock.method == "*" || mock.method.eq_ignore_ascii_case(method))
                && url_matches(&mock.url_pattern, url)
            {
                let Some(last_index) = mock.responses.len().checked_sub(1) else {
                    continue;
                };
                let index = mock.next_response.min(last_index);
                let response = mock.responses[index].clone();
                if mock.next_response < last_index {
                    mock.next_response += 1;
                }
                return Some(response);
            }
        }
        None
    })?;

    HTTP_MOCK_CALLS.with(|calls| {
        calls.borrow_mut().push(HttpMockCall {
            method: method.to_string(),
            url: url.to_string(),
            headers,
            body,
        });
    });

    Some(response)
}

/// Check if a URL matches a mock pattern (exact or glob with `*`).
pub(super) fn url_matches(pattern: &str, url: &str) -> bool {
    if pattern == "*" {
        return true;
    }
    if !pattern.contains('*') {
        return pattern == url;
    }
    // Multi-glob: split on `*` and match segments in order.
    let parts: Vec<&str> = pattern.split('*').collect();
    let mut remaining = url;
    for (i, part) in parts.iter().enumerate() {
        if part.is_empty() {
            continue;
        }
        if i == 0 {
            if !remaining.starts_with(part) {
                return false;
            }
            remaining = &remaining[part.len()..];
        } else if i == parts.len() - 1 {
            if !remaining.ends_with(part) {
                return false;
            }
            remaining = "";
        } else {
            match remaining.find(part) {
                Some(pos) => remaining = &remaining[pos + part.len()..],
                None => return false,
            }
        }
    }
    true
}

pub(super) fn redact_mock_call_url(url: &str, redact: bool) -> String {
    if !redact {
        return url.to_string();
    }
    crate::redact::current_policy().redact_url(url)
}

pub(super) fn mock_call_headers_value(
    headers: &crate::value::DictMap,
    redact_headers: bool,
) -> crate::value::DictMap {
    if !redact_headers {
        return headers.clone();
    }
    let policy = crate::redact::current_policy();
    headers
        .iter()
        .map(|(key, value)| {
            let value = if policy.header_is_sensitive(key) {
                VmValue::String(arcstr::ArcStr::from(crate::redact::REDACTED_PLACEHOLDER))
            } else {
                value.clone()
            };
            (key.clone(), value)
        })
        .collect()
}