Skip to content

Commit 31d0049

Browse files
committed
Fixing ToRpc conversion code + tests
1 parent d717baf commit 31d0049

File tree

2 files changed

+56
-37
lines changed

2 files changed

+56
-37
lines changed

src/WebJobs.Script/Rpc/MessageExtensions/RpcMessageConversionExtensions.cs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -102,30 +102,31 @@ public static TypedData ToRpc(this object value)
102102
string rawBody = null;
103103

104104
MediaTypeHeaderValue mediaType = null;
105-
MediaTypeHeaderValue.TryParse(request.ContentType, out mediaType);
106-
107-
if (string.Equals(mediaType.MediaType, "application/json", StringComparison.OrdinalIgnoreCase))
105+
if (MediaTypeHeaderValue.TryParse(request.ContentType, out mediaType))
108106
{
109-
var jsonReader = new StreamReader(request.Body, Encoding.UTF8);
110-
rawBody = jsonReader.ReadToEnd();
111-
try
107+
if (string.Equals(mediaType.MediaType, "application/json", StringComparison.OrdinalIgnoreCase))
112108
{
113-
body = JsonConvert.DeserializeObject(rawBody);
109+
var jsonReader = new StreamReader(request.Body, Encoding.UTF8);
110+
rawBody = jsonReader.ReadToEnd();
111+
try
112+
{
113+
body = JsonConvert.DeserializeObject(rawBody);
114+
}
115+
catch (JsonException)
116+
{
117+
body = rawBody;
118+
}
114119
}
115-
catch (JsonException)
120+
else if (string.Equals(mediaType.MediaType, "application/octet-stream", StringComparison.OrdinalIgnoreCase) ||
121+
mediaType.MediaType.IndexOf("multipart/", StringComparison.OrdinalIgnoreCase) >= 0)
116122
{
117-
body = rawBody;
123+
var length = Convert.ToInt32(request.ContentLength);
124+
var bytes = new byte[length];
125+
request.Body.Read(bytes, 0, length);
126+
body = bytes;
127+
rawBody = Encoding.UTF8.GetString(bytes);
118128
}
119129
}
120-
else if (string.Equals(mediaType.MediaType, "application/octet-stream", StringComparison.OrdinalIgnoreCase) ||
121-
mediaType.MediaType.IndexOf("multipart/", StringComparison.OrdinalIgnoreCase) >= 0)
122-
{
123-
var length = Convert.ToInt32(request.ContentLength);
124-
var bytes = new byte[length];
125-
request.Body.Read(bytes, 0, length);
126-
body = bytes;
127-
rawBody = Encoding.UTF8.GetString(bytes);
128-
}
129130
else
130131
{
131132
var reader = new StreamReader(request.Body, Encoding.UTF8);

test/WebJobs.Script.Tests.Integration/Host/StandbyManagerTests.cs

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,14 @@ public class StandbyManagerTests : IDisposable
3636
private TestServer _httpServer;
3737
private string _expectedHostId;
3838
private WebHostSettings _webHostSettings;
39+
private string _testRootPath;
3940

4041
public StandbyManagerTests()
4142
{
4243
_settingsManager = ScriptSettingsManager.Instance;
44+
_testRootPath = Path.Combine(Path.GetTempPath(), "StandbyManagerTests");
4345
ResetEnvironment();
46+
CleanupTestDirectory();
4447
}
4548

4649
[Fact]
@@ -110,7 +113,7 @@ public async Task StandbyMode_EndToEnd()
110113
};
111114
using (var env = new TestScopedEnvironmentVariable(vars))
112115
{
113-
await InitializeTestHost("StandbyModeTest");
116+
InitializeTestHost("Windows");
114117

115118
await VerifyWarmupSucceeds();
116119
await VerifyWarmupSucceeds(restart: true);
@@ -163,12 +166,11 @@ public async Task StandbyMode_EndToEnd_LinuxContainer()
163166
{ EnvironmentSettingNames.AzureWebsiteContainerReady, null },
164167
{ EnvironmentSettingNames.AzureWebsiteSku, "Dynamic" },
165168
{ EnvironmentSettingNames.AzureWebsiteZipDeployment, null },
166-
{ EnvironmentSettingNames.AzureWebJobsSecretStorageType, "Blob" },
167169
{ "AzureWebEncryptionKey", "0F75CA46E7EBDD39E4CA6B074D1F9A5972B849A55F91A248" }
168170
};
169171
using (var env = new TestScopedEnvironmentVariable(vars))
170172
{
171-
await InitializeTestHost("StandbyModeTest_Linux");
173+
InitializeTestHost("Linux");
172174

173175
await VerifyWarmupSucceeds();
174176
await VerifyWarmupSucceeds(restart: true);
@@ -178,12 +180,21 @@ public async Task StandbyMode_EndToEnd_LinuxContainer()
178180

179181
// immediately call a function - expect the call to block until
180182
// the host is fully specialized
183+
// the Unauthorized is expected since we havne't specified the key
184+
// it's enough here to ensure we don't get a 404
185+
var request = new HttpRequestMessage(HttpMethod.Get, $"api/httptrigger");
186+
request.Headers.Add(ScriptConstants.AntaresColdStartHeaderName, "1");
187+
var response = await _httpClient.SendAsync(request);
188+
Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
189+
190+
// now that the host is initialized, send a valid key
191+
// and expect success
181192
var secretManager = _httpServer.Host.Services.GetService<ISecretManager>();
182193
var keys = await secretManager.GetFunctionSecretsAsync("HttpTrigger");
183194
string key = keys.First().Value;
184-
var request = new HttpRequestMessage(HttpMethod.Get, $"api/httptrigger?code={key}");
195+
request = new HttpRequestMessage(HttpMethod.Get, $"api/httptrigger?code={key}");
185196
request.Headers.Add(ScriptConstants.AntaresColdStartHeaderName, "1");
186-
var response = await _httpClient.SendAsync(request);
197+
response = await _httpClient.SendAsync(request);
187198
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
188199

189200
Assert.False(WebScriptHostManager.InStandbyMode);
@@ -226,34 +237,26 @@ public async Task StandbyMode_EndToEnd_LinuxContainer()
226237
}
227238
}
228239

229-
private async Task InitializeTestHost(string testDirName)
240+
private void InitializeTestHost(string testDirName)
230241
{
231242
var httpConfig = new HttpConfiguration();
232-
var testRootPath = Path.Combine(Path.GetTempPath(), testDirName);
233-
try
234-
{
235-
await FileUtility.DeleteDirectoryAsync(testRootPath, true);
236-
}
237-
catch
238-
{
239-
// best effort cleanup
240-
}
243+
var uniqueTestRootPath = Path.Combine(_testRootPath, testDirName, Guid.NewGuid().ToString());
241244

242245
_loggerProvider = new TestLoggerProvider();
243246
var loggerProviderFactory = new TestLoggerProviderFactory(_loggerProvider);
244247
_webHostSettings = new WebHostSettings
245248
{
246249
IsSelfHost = true,
247-
LogPath = Path.Combine(testRootPath, "Logs"),
248-
SecretsPath = Path.Combine(testRootPath, "Secrets"),
249-
ScriptPath = Path.Combine(testRootPath, "WWWRoot")
250+
LogPath = Path.Combine(uniqueTestRootPath, "Logs"),
251+
SecretsPath = Path.Combine(uniqueTestRootPath, "Secrets"),
252+
ScriptPath = Path.Combine(uniqueTestRootPath, "WWWRoot")
250253
};
251254

252255
if (_settingsManager.IsAppServiceEnvironment)
253256
{
254257
// if the test is mocking App Service environment, we need
255258
// to also set the HOME variable
256-
Environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteHomePath, testRootPath);
259+
Environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteHomePath, uniqueTestRootPath);
257260
}
258261

259262
var loggerFactory = new LoggerFactory();
@@ -373,12 +376,27 @@ private static async Task<Uri> CreateBlobSas(string filePath, string blobContain
373376
public void Dispose()
374377
{
375378
ResetEnvironment();
379+
CleanupTestDirectory();
380+
}
381+
382+
private void CleanupTestDirectory()
383+
{
384+
var testRootPath = Path.Combine(Path.GetTempPath(), "StandbyManagerTests");
385+
try
386+
{
387+
FileUtility.DeleteDirectoryAsync(testRootPath, true);
388+
}
389+
catch
390+
{
391+
// best effort cleanup
392+
}
376393
}
377394

378395
private void ResetEnvironment()
379396
{
380397
Environment.SetEnvironmentVariable(EnvironmentSettingNames.ContainerName, string.Empty);
381398
Environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteZipDeployment, string.Empty);
399+
Environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteHomePath, null);
382400
WebScriptHostManager.ResetStandbyMode();
383401
}
384402
}

0 commit comments

Comments
 (0)