Skip to content

Commit

Permalink
Improve how expectations for headers and query params are presented
Browse files Browse the repository at this point in the history
  • Loading branch information
cezarypiatek committed Nov 2, 2023
1 parent 8817c5a commit 40d9ca1
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 41 deletions.
30 changes: 26 additions & 4 deletions src/WireMockInspector/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,14 @@ private static MappingDetails GetMappingDetails(RequestViewModel req, MappingMod
},
Expectations = expectations.Request?.Headers switch
{
IList<HeaderModel> headers => ExpectationsAsJson(headers),
IList<HeaderModel> headers => new GridExpectations()
{
Items = headers.Select(x=> new GridExpectationItem
{
Name = x.Name,
Matchers = x.Matchers!=null ? MapToRichExpectations(x, x.Matchers.ToArray(), x.MatchOperator).Matchers: Array.Empty<ExpectationMatcher>()
}).ToList()
},
_ => MissingExpectations.Instance
},
NoExpectations = expectations.Request?.Headers is null
Expand All @@ -793,7 +800,18 @@ private static MappingDetails GetMappingDetails(RequestViewModel req, MappingMod
{
Items = req.Raw.Request.Query?.OrderBy(x=>x.Key).SelectMany(x=> x.Value.Select(v => new KeyValuePair<string,string>(x.Key, v))).ToList() ?? new List<KeyValuePair<string, string>>(),
},
Expectations = ExpectationsAsJson(expectations.Request?.Params),
Expectations = expectations.Request?.Params switch
{
IList<ParamModel> paramModels => new GridExpectations()
{
Items = paramModels.Select(x=> new GridExpectationItem
{
Name = x.Name,
Matchers = x.Matchers!=null ? MapToRichExpectations(x, x.Matchers.ToArray(), null).Matchers: Array.Empty<ExpectationMatcher>()
}).ToList()
},
_ => MissingExpectations.Instance
},
NoExpectations = expectations.Request?.Params is null
},
new()
Expand Down Expand Up @@ -904,7 +922,7 @@ private static ExpectationsModel MapToRichExpectations(BodyModel? requestBody)
return MapToRichExpectations(requestBody, matchers, requestBody.MatchOperator);
}

private static ExpectationsModel MapToRichExpectations(object definition, MatcherModel[] matchers, string? matchOperator)
private static RichExpectations MapToRichExpectations(object definition, IReadOnlyList<MatcherModel> matchers, string? matchOperator)
{
IEnumerable<string> GetPatterns(MatcherModel m)
{
Expand Down Expand Up @@ -968,7 +986,11 @@ IEnumerable<string> GetTags(MatcherModel m)
new KeyValuePair<string, string>("Operator", x.MatchOperator?.ToString()),
}.Where(x => string.IsNullOrWhiteSpace(x.Value) == false).ToList(),
Tags = GetTags(x).ToList(),
Patterns = GetPatterns(x).Select(y => new MarkdownCode("json", y).TryToReformat()).ToList()
Patterns = GetPatterns(x).Select(y => y.Trim() switch
{
var v when v.StartsWith("{") || v.StartsWith("[") => (Text) new MarkdownCode("json", y).TryToReformat(),
_ => (Text)new SimpleText(y)
} ).ToList()
}).ToList()
};
}
Expand Down
18 changes: 16 additions & 2 deletions src/WireMockInspector/ViewModels/MatchDetailsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class ExpectationMatcher

public IReadOnlyList<KeyValuePair<string,string>> Attributes { get; set; }
public List<string> Tags { get; set; }
public List<MarkdownCode> Patterns { get; set; }
public List<Text> Patterns { get; set; }
}

public abstract class ExpectationsModel
Expand All @@ -30,6 +30,17 @@ public class SimpleKeyValueExpectations: ExpectationsModel

}

public class GridExpectationItem
{
public string Name { get; set; }
public IReadOnlyList<ExpectationMatcher> Matchers { get; set; }
}
public class GridExpectations: ExpectationsModel
{
public IReadOnlyList<GridExpectationItem> Items { get; set; }

}

class SimpleStringExpectations : ExpectationsModel
{
public string Value { get; set; }
Expand Down Expand Up @@ -143,7 +154,10 @@ public class MarkdownActualValue:ActualValue
public string MarkdownValue { get; set; }
}

public record MarkdownCode(string lang, string rawValue)
public record Text();

public record SimpleText(string Value):Text;
public record MarkdownCode(string lang, string rawValue):Text
{
public string AsMarkdownSyntax()
{
Expand Down
125 changes: 90 additions & 35 deletions src/WireMockInspector/Views/RequestDetails.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
</Expander.Header>

<StackPanel Orientation="Vertical">
<Border BorderThickness="0,0,0,1" BorderBrush="#808080" Margin="0,0,0,15">
<Border BorderThickness="0,0,0,1" BorderBrush="#808080" Margin="0,0,0,10">
<DockPanel HorizontalAlignment="Stretch">
<Label DockPanel.Dock="Left" VerticalAlignment="Center" >Actual value</Label>
<SplitButton HorizontalAlignment="Right" Margin="0,0,5,5" Padding="0">
Expand Down Expand Up @@ -93,25 +93,23 @@
</Style>
</DataGrid.Styles>
<DataGrid.Columns>
<DataGridTextColumn Header="Key" Binding="{Binding Key}"/>
<DataGridTextColumn Header="Value" Binding="{Binding Value}" />
<DataGridTextColumn Header="Key" Width="Auto" Binding="{Binding Key}"/>
<DataGridTextColumn Header="Value" Width="*" Binding="{Binding Value}" />
</DataGrid.Columns>
</DataGrid>
</DataTemplate>

</ContentControl.DataTemplates>
</ContentControl>
<Border BorderThickness="0,0,0,1" BorderBrush="#808080" Margin="0,15,0,5">
<Border BorderThickness="0,0,0,1" BorderBrush="#808080" Margin="0,15,0,10">
<DockPanel HorizontalAlignment="Stretch">
<Label DockPanel.Dock="Left" VerticalAlignment="Center" >Expectations</Label>
</DockPanel>
</Border>
<ContentControl Content="{Binding Expectations}">
<ContentControl.DataTemplates>
<DataTemplate x:DataType="viewModels:RichExpectations">
<TabControl Classes="minor">
<TabItem Header="Formatted" >
<ItemsControl ItemsSource="{Binding Matchers}" >
<ItemsControl ItemsSource="{Binding Matchers}" >
<ItemsControl.ItemTemplate>
<DataTemplate x:DataType="viewModels:ExpectationMatcher">
<StackPanel Orientation="Vertical">
Expand All @@ -129,42 +127,26 @@
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<!-- <DataGrid ItemsSource="{Binding Attributes}" HeadersVisibility="None" CanUserReorderColumns="False" CanUserResizeColumns="True" AutoGenerateColumns="False" AreRowGroupHeadersFrozen="True" GridLinesVisibility="All" ClipboardCopyMode="ExcludeHeader" SelectionMode="Single"> -->
<!-- <DataGrid.Styles> -->
<!-- <Style Selector="DataGridColumnHeader"> -->
<!-- <Setter Property="Background" Value="#505050"></Setter> -->
<!-- </Style> -->
<!-- <Style Selector="DataGridColumnHeader TextBlock"> -->
<!-- <Setter Property="Foreground" Value="White"></Setter> -->
<!-- </Style> -->
<!-- <Style Selector="DataGridCell TextBlock"> -->
<!-- <Setter Property="FontSize" Value="10"></Setter> -->
<!-- </Style> -->
<!-- <Style Selector="DataGridCell.valuecell TextBlock"> -->
<!-- <Setter Property="TextAlignment" Value="Center"></Setter> -->
<!-- </Style> -->
<!-- </DataGrid.Styles> -->
<!-- <DataGrid.Columns> -->
<!-- <DataGridTextColumn Header="Key" Binding="{Binding Key}"/> -->
<!-- <DataGridTextColumn Header="Value" Binding="{Binding Value}" CellStyleClasses="valuecell"/> -->
<!-- </DataGrid.Columns> -->
<!-- </DataGrid> -->
<ItemsControl ItemsSource="{Binding Patterns}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<views:CodeBlockViewer Margin="0,5,0,0" Code="{Binding }" />
<ContentControl Content="{Binding}">
<ContentControl.DataTemplates>
<DataTemplate x:DataType="viewModels:MarkdownCode">
<views:CodeBlockViewer Margin="0,5,0,0" Code="{Binding }" />
</DataTemplate>
<DataTemplate x:DataType="viewModels:SimpleText">
<SelectableTextBlock Margin="5" Text="{Binding Value}" FontWeight="Bold"></SelectableTextBlock>
</DataTemplate>
</ContentControl.DataTemplates>
</ContentControl>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</TabItem>
<TabItem Header="Raw">
<views:CodeBlockViewer Code="{Binding Definition}" />
</TabItem>
</TabControl>
</DataTemplate>
<DataTemplate x:DataType="viewModels:RawExpectations">
<StackPanel >
Expand All @@ -191,8 +173,81 @@
</Style>
</DataGrid.Styles>
<DataGrid.Columns>
<DataGridTextColumn Header="Key" Binding="{Binding Key}"/>
<DataGridTextColumn Header="Value" Binding="{Binding Value}" />
<DataGridTextColumn Header="Key" Width="Auto" Binding="{Binding Key}"/>
<DataGridTextColumn Header="Value" Width="*" Binding="{Binding Value}" />
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
<DataTemplate x:DataType="viewModels:GridExpectations">
<DataGrid ItemsSource="{Binding Items}" VerticalScrollBarVisibility="Disabled" CanUserReorderColumns="False" CanUserResizeColumns="True" AutoGenerateColumns="False" AreRowGroupHeadersFrozen="True" GridLinesVisibility="All" ClipboardCopyMode="ExcludeHeader" SelectionMode="Single">
<DataGrid.Styles>
<Style Selector="DataGridColumnHeader">
<Setter Property="Background" Value="#505050"></Setter>
</Style>
<Style Selector="DataGridColumnHeader TextBlock">
<Setter Property="Foreground" Value="White"></Setter>
</Style>
<Style Selector="DataGridCell TextBlock">
<Setter Property="FontSize" Value="10"></Setter>
</Style>
</DataGrid.Styles>
<DataGrid.Columns>
<DataGridTextColumn Header="Key" IsReadOnly="True" Width="Auto" Binding="{Binding Name}"/>
<DataGridTemplateColumn Header="Patterns" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding Matchers}" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate x:DataType="viewModels:ExpectationMatcher">
<StackPanel Orientation="Vertical">
<ItemsControl ItemsSource="{Binding Tags}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" Margin="12,5,12,5"></WrapPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border BorderThickness="1" BorderBrush="Wheat" CornerRadius="5" Margin="0,5,5,0">
<TextBlock Margin="5" Text="{Binding}"></TextBlock>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<ItemsControl ItemsSource="{Binding Patterns}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<ContentControl Content="{Binding}">
<ContentControl.DataTemplates>
<DataTemplate x:DataType="viewModels:MarkdownCode">
<SelectableTextBlock Margin="12,5" FontSize="10" Text="{Binding rawValue}"></SelectableTextBlock>
</DataTemplate>
<DataTemplate x:DataType="viewModels:SimpleText">
<SelectableTextBlock Margin="12, 5" FontSize="10" Text="{Binding Value}"></SelectableTextBlock>
</DataTemplate>
</ContentControl.DataTemplates>
</ContentControl>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

</DataGrid.Columns>
</DataGrid>
</DataTemplate>
Expand Down

0 comments on commit 40d9ca1

Please sign in to comment.