Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,32 @@ public void Invalid_syntax_produces_diagnostics(ISyntaxSpec syntaxSpec, int inde
syntaxSpec.Validate(parseResult.SyntaxTree.RootNode.ChildNodes.Single());
}

[Fact]
public void DEBUG_ME()
{
var code = """

hptps://example.com
Authorization: Basic {{token}}
Cookie: {{cookie}}


{
"number": {{numberValue}},
"string":
{{stringValue}}
}


""";

var result = Parse(code);

result.SyntaxTree.RootNode.ChildNodes.Should().ContainSingle<HttpRequestNode>()
.Which.ChildNodes.Should().ContainSingle<HttpUrlNode>()
.Which.Text.Should().Be("hptps://example.com");
}

public static IEnumerable<object[]> GenerateValidRequests()
{
var i = 0;
Expand Down Expand Up @@ -99,9 +125,6 @@ public static IEnumerable<object[]> GenerateInvalidRequests()
};
}


// FIX: (GenerateInvalidRequests)

foreach (var method in ValidMethods())
foreach (var url in InvalidUrls())
foreach (var version in ValidVersions())
Expand All @@ -116,7 +139,6 @@ public static IEnumerable<object[]> GenerateInvalidRequests()
};
}

// FIX: (GenerateInvalidRequests)
foreach (var method in ValidMethods())
foreach (var url in ValidUrls())
foreach (var version in InvalidVersions())
Expand All @@ -131,19 +153,19 @@ public static IEnumerable<object[]> GenerateInvalidRequests()
};
}

// foreach (var method in ValidMethods())
// foreach (var url in ValidUrls())
// foreach (var version in ValidVersions())
// foreach (var headerSection in InvalidHeaderSections())
// foreach (var bodySection in ValidBodySections())
// {
// ++i;
// yield return new object[]
// {
// new HttpRequestNodeSyntaxSpec(method, url, version, headerSection, bodySection),
// i
// };
// }
foreach (var method in ValidMethods())
foreach (var url in ValidUrls())
foreach (var version in ValidVersions())
foreach (var headerSection in InvalidHeaderSections())
foreach (var bodySection in ValidBodySections())
{
++i;
yield return new object[]
{
new HttpRequestNodeSyntaxSpec(method, url, version, headerSection, bodySection),
i
};
}
}

private static IEnumerable<HttpMethodNodeSyntaxSpec> ValidMethods()
Expand Down Expand Up @@ -172,8 +194,8 @@ private static IEnumerable<HttpUrlNodeSyntaxSpec> ValidUrls()

private static IEnumerable<HttpUrlNodeSyntaxSpec> InvalidUrls()
{
yield return new("hptps://example.com");
// FIX: (InvalidUrls) yield return new("http://example .com");
// Misspelled
yield return new("hptps://example.com");
}

private static IEnumerable<HttpVersionNodeSyntaxSpec> ValidVersions()
Expand All @@ -184,7 +206,10 @@ private static IEnumerable<HttpVersionNodeSyntaxSpec> ValidVersions()

private static IEnumerable<HttpVersionNodeSyntaxSpec> InvalidVersions()
{
// Misspellled
yield return new("HTPT");

// The space is invalid
yield return new("HTTP 1.1");
}

Expand Down Expand Up @@ -215,6 +240,7 @@ private static IEnumerable<HttpHeadersNodeSyntaxSpec> ValidHeaderSections()

private static IEnumerable<HttpHeadersNodeSyntaxSpec> InvalidHeaderSections()
{
// The space in the header name is invalid
yield return new("""
Accept: */*
Accept Encoding: gzip, deflate, br
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,6 @@ public void Common_headers_are_bound_correctly()

}



[Fact]
public void Diagnostics_are_reported_for_missing_header_expression_values()
{
Expand All @@ -187,7 +185,7 @@ public void Missing_header_name_produces_a_diagnostic()
{
var result = Parse(
"""
GET https://example.com
GET https://example.com
: {{accept}}
""");

Expand All @@ -196,6 +194,20 @@ public void Missing_header_name_produces_a_diagnostic()
headerNode.GetDiagnostics().Should().ContainSingle().Which.Message.Should().Be("Missing header name");
}

[Fact]
public void Whitespace_in_a_header_name_produces_a_diagnostic()
{
var result = Parse(
"""
GET https://example.com
Content Type: text/html
""");

var headerNode = result.SyntaxTree.RootNode.DescendantNodesAndTokens().Should().ContainSingle<HttpHeaderNode>().Which;

headerNode.GetDiagnostics().Should().ContainSingle().Which.Message.Should().Be("Invalid whitespace in header name");
}

[Fact]
public void Missing_header_value_produces_a_diagnostic()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,9 @@ public override IEnumerable<Diagnostic> GetDiagnostics()
{
yield return CreateDiagnostic("Missing header name");
}
else if (TextContainsWhitespace())
{
yield return CreateDiagnostic("Invalid whitespace in header name");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,8 @@ private HttpHeaderNameNode ParseHeaderName()
{
var node = new HttpHeaderNameNode(_sourceText, _syntaxTree);

if (MoreTokens() && CurrentToken.Kind is HttpTokenKind.Word)
if (MoreTokens() &&
CurrentToken is not { Kind: HttpTokenKind.NewLine } and not { Kind: HttpTokenKind.Punctuation, Text: ":" })
{
ParseLeadingTrivia(node);

Expand All @@ -445,11 +446,6 @@ private HttpHeaderNameNode ParseHeaderName()

while (MoreTokens())
{
if (CurrentToken.Kind is HttpTokenKind.Whitespace or HttpTokenKind.NewLine)
{
break;
}

if (CurrentToken is { Kind: HttpTokenKind.Punctuation } and { Text: ":" })
{
break;
Expand Down Expand Up @@ -509,19 +505,7 @@ private HttpHeaderValueNode ParseHeaderValue()
var node = new HttpBodySeparatorNode(_sourceText, _syntaxTree);

ParseLeadingTrivia(node);

if (MoreTokens() && CurrentToken.Kind is HttpTokenKind.Whitespace or HttpTokenKind.NewLine &&
!IsRequestSeparator())
{
ConsumeCurrentTokenInto(node);

while (MoreTokens() && CurrentToken.Kind is (HttpTokenKind.Whitespace or HttpTokenKind.NewLine) &&
!IsRequestSeparator())
{
ConsumeCurrentTokenInto(node);
}
}


return ParseTrailingTrivia(node);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ private protected HttpSyntaxNode(
};
}

protected bool TextContainsWhitespace()
{
// ignore whitespace if it's the first or last token
for (var i = 1; i < _childNodesAndTokens.Count - 1; i++)
{
var nodeOrToken = _childNodesAndTokens[i];
if (nodeOrToken is HttpSyntaxToken { Kind: HttpTokenKind.Whitespace })
{
return true;
}
}

return false;
}

private void GrowSpan(HttpSyntaxNodeOrToken child)
{
if (_fullSpan == default)
Expand Down Expand Up @@ -145,13 +160,11 @@ public override IEnumerable<Diagnostic> GetDiagnostics()
}
}

if (_diagnostics is not null)
foreach (var diagnostic in base.GetDiagnostics())
{
foreach (var diagnostic in _diagnostics)
{
yield return diagnostic;
}
yield return diagnostic;
}

}

public IEnumerable<HttpSyntaxNodeOrToken> DescendantNodesAndTokensAndSelf()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,16 @@ private protected HttpSyntaxNodeOrToken(SourceText sourceText, HttpSyntaxTree? s

public override string ToString() => $"{GetType().Name}: {Text}";

public abstract IEnumerable<Diagnostic> GetDiagnostics();
public virtual IEnumerable<Diagnostic> GetDiagnostics()
{
if (_diagnostics is not null)
{
foreach (var diagnostic in _diagnostics)
{
yield return diagnostic;
}
}
}

public void AddDiagnostic(Diagnostic d)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,4 @@ internal HttpSyntaxToken(
public HttpTokenKind Kind { get; set; }

public override string ToString() => $"{Kind}: {Text}";

public override IEnumerable<Diagnostic> GetDiagnostics()
{
if (_diagnostics is not null)
{
foreach (var diagnostic in _diagnostics)
{
yield return diagnostic;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public override IEnumerable<Diagnostic> GetDiagnostics()
yield return CreateDiagnostic("Invalid HTTP version");
}

if (ChildNodesAndTokens.OfType<HttpSyntaxToken>().Any(t => t.Kind is HttpTokenKind.Whitespace))
if (TextContainsWhitespace())
{
yield return CreateDiagnostic("Invalid HTTP version");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
<NamedTestSelector>
<TestName>Microsoft.DotNet.Interactive.PowerShell.Tests.PowerShellKernelTests.GetCorrectProfilePaths</TestName>
</NamedTestSelector>
<NamedTestSelector>
<TestName>Microsoft.DotNet.Interactive.PowerShell.Tests.PowerShellKernelTests.When_code_produces_errors_then_the_command_fails</TestName>
</NamedTestSelector>
</IgnoredTests>
</Settings>
</ProjectConfiguration>