Last active
May 1, 2024 19:16
-
-
Save nikanos/34e27eb054f85d38663132ae0f133eeb to your computer and use it in GitHub Desktop.
LINQPad 5 (.NET Framework) Query to mock API Responses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Query Kind="Program"> | |
<Reference><RuntimeDirectory>\System.Net.Http.dll</Reference> | |
<Reference><RuntimeDirectory>\System.Windows.Forms.dll</Reference> | |
<NuGetReference>Microsoft.AspNet.WebApi.OwinSelfHost</NuGetReference> | |
<Namespace>Microsoft.Owin.Hosting</Namespace> | |
<Namespace>Owin</Namespace> | |
<Namespace>System.Net</Namespace> | |
<Namespace>System.Net.Http</Namespace> | |
<Namespace>System.Threading.Tasks</Namespace> | |
<Namespace>System.Web.Http</Namespace> | |
<Namespace>System.Web.Http.Controllers</Namespace> | |
<Namespace>System.Web.Http.Dispatcher</Namespace> | |
<Namespace>System.Windows.Forms</Namespace> | |
</Query> | |
void Main() | |
{ | |
var app = WebApp.Start<Startup>(Options.BASE_ADDRESS); | |
//If the query runs under LPRun then CMD should be defined and the Console handling code should be executed. | |
#if CMD | |
Console.WriteLine("Press Enter to exit..."); | |
Console.ReadLine(); | |
app.Dispose(); | |
Console.WriteLine("Exiting!!!"); | |
#else | |
var button = new Button { Text = "Click to stop server." }; | |
LINQPad.OutputPanel panel = LINQPad.PanelManager.DisplayControl(button, "Webserver"); | |
button.Click += (o, e) => | |
{ | |
app.Dispose(); | |
panel.Close(); | |
}; | |
#endif | |
} | |
class Options | |
{ | |
public const string BASE_ADDRESS = "http://localhost:8080"; | |
public const string MOCK_RESPONSES_DIRECTORY = @"C:\Temp\MockResponses"; | |
public const string MOCK_RESPONSE_FILENAME = "mock_response_"; | |
public const string MOCK_RESPONSE_DEFAULT_SUFFIX = "default"; | |
} | |
public class ControllerResolver : DefaultHttpControllerTypeResolver | |
{ | |
public override ICollection<Type> GetControllerTypes(IAssembliesResolver assembliesResolver) | |
{ | |
var types = Assembly.GetExecutingAssembly().GetExportedTypes(); | |
return types.Where(i => typeof(IHttpController).IsAssignableFrom(i)).ToList(); | |
} | |
} | |
public class Startup | |
{ | |
public void Configuration(IAppBuilder appBuilder) | |
{ | |
var config = new HttpConfiguration(); | |
//LINQPad classes are nested so we use the ControllerResolver instead of the default one | |
config.Services.Replace(typeof(IHttpControllerTypeResolver), new ControllerResolver()); | |
//Use attributes for routing | |
config.MapHttpAttributeRoutes(); | |
appBuilder.UseWebApi(config); | |
} | |
} | |
[RoutePrefix("api")] | |
public class MyController : ApiController | |
{ | |
[Route("mock-response/{responseId?}")] | |
[HttpGet] | |
public async Task<IHttpActionResult> HandleMockRequest(int? responseId = null) | |
{ | |
string requestString = await Request.Content.ReadAsStringAsync(); | |
string responseFileName = Options.MOCK_RESPONSE_FILENAME + (responseId.HasValue ? responseId.ToString() : Options.MOCK_RESPONSE_DEFAULT_SUFFIX); | |
string responseFullFileName = Path.Combine(Options.MOCK_RESPONSES_DIRECTORY, responseFileName); | |
string responseContent = File.Exists(responseFullFileName) ? File.ReadAllText(responseFullFileName, Encoding.UTF8) : "<root>non-existing-response</root>"; | |
HttpResponseMessage httpResponse = new HttpResponseMessage(HttpStatusCode.OK) | |
{ | |
Content = new StringContent(responseContent, Encoding.UTF8, "application/xml") | |
}; | |
httpResponse.Headers.CacheControl = new System.Net.Http.Headers.CacheControlHeaderValue | |
{ | |
NoCache = true, | |
NoStore = true, | |
MustRevalidate = true, | |
MaxAge = TimeSpan.Zero | |
}; | |
return ResponseMessage(httpResponse); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment