Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

value kernel honours content type in --from-url #2771

Merged
merged 1 commit into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -191,7 +191,7 @@ public async Task It_can_import_URL_contents_as_strings()
{
using var kernel = CreateKernel();

await kernel.SubmitCodeAsync("#!value --name hi --from-url http://bing.com");
await kernel.SubmitCodeAsync("#!value --name hi --from-url https://bing.com");

var keyValueStoreKernel = kernel.FindKernelByName("value");

Expand All @@ -202,6 +202,22 @@ public async Task It_can_import_URL_contents_as_strings()
.Contain("<html");
}

[Fact]
public async Task When_import_URL_contents_the_mimetype_is_preserved()
{
using var kernel = CreateKernel();

await kernel.SubmitCodeAsync("#!value --name hi --from-url https://bing.com");

var keyValueStoreKernel = kernel.FindKernelByName("value");

var valueProduced = await keyValueStoreKernel.RequestValueAsync("hi");

valueProduced.FormattedValue.MimeType
.Should()
.Be("text/html");
}

[Fact]
public async Task It_can_store_user_input()
{
Expand Down
20 changes: 13 additions & 7 deletions src/Microsoft.DotNet.Interactive/KeyValueStoreKernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ internal async Task TryStoreValueFromOptionsAsync(
KernelInvocationContext context,
ValueDirectiveOptions options)
{
var mimeType = options.MimeType;
string newValue = null;
var loadedFromOptions = false;

Expand All @@ -104,6 +105,7 @@ internal async Task TryStoreValueFromOptionsAsync(
var client = new HttpClient();
var response = await client.GetAsync(uri, context.CancellationToken);
newValue = await response.Content.ReadAsStringAsync();
mimeType ??= response.Content.Headers?.ContentType?.MediaType;
loadedFromOptions = true;
}
else if (options.FromValue is { } value)
Expand All @@ -118,7 +120,7 @@ internal async Task TryStoreValueFromOptionsAsync(

_lastOperation = (hadValue, previousValue, newValue);

StoreValue(newValue, options, context);
StoreValue(newValue, options, context, mimeType: mimeType);
}
else
{
Expand All @@ -130,7 +132,8 @@ private void StoreValue(
KernelCommand command,
KernelInvocationContext context,
ValueDirectiveOptions options,
string value = null)
string value = null,
string mimeType = null)
{
if (options.FromFile is { })
{
Expand All @@ -153,7 +156,7 @@ private void StoreValue(
}
else
{
StoreValue(value, options, context);
StoreValue(value, options, context, mimeType: mimeType);
}

_lastOperation = default;
Expand Down Expand Up @@ -181,13 +184,16 @@ void UndoSetValue()
private void StoreValue(
string value,
ValueDirectiveOptions options,
KernelInvocationContext context)
KernelInvocationContext context,
string mimeType = null)

{
_values[options.Name] = new FormattedValue(options.MimeType ?? PlainTextFormatter.MimeType, value);
mimeType ??= ( options.MimeType ?? PlainTextFormatter.MimeType);
_values[options.Name] = new FormattedValue(mimeType, value);

if (options.MimeType is { } mimeType)
if (options.MimeType is {} displayMimeType)
{
context.DisplayAs(value, mimeType);
context.DisplayAs(value, displayMimeType);
}
}
}