Skip to content
This repository has been archived by the owner on Dec 18, 2018. It is now read-only.

Call OnStarting before verifying response length (#1289) #1302

Merged
merged 2 commits into from
Jan 13, 2017

Conversation

cesarblum
Copy link
Contributor

var writeException = await Assert.ThrowsAsync<ObjectDisposedException>(async () =>
await response.Body.WriteAsync(Encoding.ASCII.GetBytes("Hello World"), 0, 11));

var writeException = await Assert.ThrowsAsync<ObjectDisposedException>(async () => await response.Body.FlushAsync());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to change so the check at

wouldn't close the connection (because now the write doesn't go through before OnStarting as it did before, that method sees 0 bytes written and that's a mismatch against the old test which wrote 11 bytes).

Copy link
Member

@halter73 halter73 Jan 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems bad. So you don't get a 500 anymore if you set a Content-Length?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You do. The point here is to not only test that you get a 500 when an OnStarting callback throws, but also that the connection is not closed in that situation. Because the test code captures the exception from WriteAsync and does not rethrow it, we end up verifying the amount of bytes written after the app runs (see link above), which also leads to a 500 response but this one also closes the connection (because too few bytes were written and that will confuse the client).

Switching this to FlushAsync() makes the test simpler and avoids that situation. An alternative would be to keep the WriteAsync call and rethrow the exception, but this will result in more errors being logged and will require changing the assertion for that.

@cesarblum
Copy link
Contributor Author

🔔

@natemcmaster natemcmaster self-requested a review January 11, 2017 23:01
Copy link
Contributor

@natemcmaster natemcmaster left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any issues with this change. :shipit:

}
else
{
VerifyAndUpdateWrite(data.Count);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we change InitializeResponse to not call VerifyAndUpdateWrite, and just call VerifyAndUpdateWrite afterwards?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need the check in InitializeResponse so we can return a 500 if the first write exceeds Content-Length. Without the check there a 200 response still started, despite the code failing later.

httpContext.Response.ContentLength = 5;
await httpContext.Response.WriteAsync("hello, world");
httpContext.Response.Body.Write(response, 0, response.Length);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why no longer async?

Copy link
Contributor Author

@cesarblum cesarblum Jan 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The WriteAsync version of this test is just below. Need to verify both calls.

@cesarblum
Copy link
Contributor Author

Updated.

@halter73
Copy link
Member

:shipit:

@cesarblum cesarblum merged commit 89a63d1 into dev Jan 13, 2017
@cesarblum cesarblum deleted the cesarbs/onstarting-before-verifywrite branch January 13, 2017 19:14
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants