Add ImportFromCurl (closes #124)#135
Draft
dennisdoomen wants to merge 1 commit into
Draft
Conversation
…124) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
| request.Headers.Add(new KeyValuePair<string, string>(name, value)); | ||
| } | ||
|
|
||
| private CurlRequest Build(string curlCommand) |
|
|
||
| if (request.Headers.TryGetValues(name, out IEnumerable<string>? values)) | ||
| { | ||
| return values.Any(v => string.Equals(v, value, StringComparison.Ordinal)) || |
| if (request.Headers.TryGetValues(name, out IEnumerable<string>? values)) | ||
| { | ||
| return values.Any(v => string.Equals(v, value, StringComparison.Ordinal)) || | ||
| string.Equals(string.Join(", ", values), value, StringComparison.Ordinal); |
| private bool tokenStarted; | ||
| private int position; | ||
|
|
||
| public CurlTokenizer(string text) |
Coverage Report for CI Build 26678625011Coverage decreased (-0.9%) to 82.733%Details
Uncovered Changes
Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #124
Summary
Adds
HttpMock.ImportFromCurl(string curlCommand)to bootstrap a request mock from an existingcurlcommand — for example the output of a browser's "Copy as cURL" or a snippet from API docs. The parsed request is translated into the equivalent fluent matching configuration and aRequestMockBuilderis returned so a response can be attached:This PR is scoped to cURL only (Phase 1). HAR import is deferred to a follow-up PR (see the issue's Phase 2). No new runtime dependency is introduced — parsing uses only the BCL and
System.Text.Json.New public API
Mockly.HttpMock.ImportFromCurl(string curlCommand) : RequestMockBuilder(API verification files under
Mockly.ApiVerificationTests/ApprovedApiwere regenerated viaAcceptApiChanges.ps1.)What gets parsed
-X/--request. Defaults toPOSTwhen a body is present, otherwiseGET.--url; scheme, host, path and query are mapped onto the builder.-H/--header(plus-A/--user-agent,-e/--referer,-b/--cookie).Content-Typeis matched against the request's content type (parameters such ascharsetignored).-d/--data/--data-raw/--data-ascii/--data-binary/--data-urlencode. JSON bodies are matched whitespace-insensitively; other bodies are matched exactly. Multiple data options are joined with&.Shell syntax handling
\" \\ \$ \`` escapes), and line continuations (`,^,`followed by a newline).curltoken is ignored.-u user:pass,-o,--connect-timeout) are consumed so their value is not mistaken for the URL. Other unknown flags are ignored.ArgumentException(ArgumentNullExceptionfornull).Tests
Added
Mockly.Specs/CurlImportSpecs.cs(xUnit + FluentAssertions, AAA, nested classes) covering GET, query strings, explicit/lowercase/defaulted methods, single & multiple headers, JSON and form-encoded bodies, exact-body negative matching, value-taking option handling, line continuations and double quotes, plus malformed-input cases. Full suite is green onnet8.0(111) andnet472(110).Notes for reviewers
-Hbecomes a matcher. Browser "Copy as cURL" output includes volatile headers (User-Agent,Accept,Sec-*), so an imported mock can be stricter than a plainHttpClientrequest. Happy to soften this (e.g. only match a curated subset by default) if preferred.Content-Length,Content-Encoding) are skipped rather than turned into matchers that could never pass.--data @file,--data-urlencodeencoding, and-Gare not specially handled yet; can be added if in scope.🤖 Generated with GitHub Copilot CLI