Having Get single and Get all methods on a ApiController - asp.net-mvc-routing

I'm developing an API using close to the latest bits from the aspnetwebstack Codeplex project (4592e2f63c55 from 2012-05-09 if anyone is interested).
I have the following route:
context.Routes.MapHttpRoute("SiteSpecific", "Api/{controller}/{customerId}/{siteToken}/{id}",
new { id = UrlParameter.Optional });
And what I'm currently trying to do is implement get single and a get all in an ApiController. The Get all method, for testing is the following:
public IEnumerable<EditChatResponse> Get(string customerId, string siteToken)
{
return new []{new EditChatResponse{Template = "Get All"}, };
}
And the get single is currently following:
public EditChatResponse Get(string customerId, string siteToken, string id)
{
return new EditChatResponse {Template = "Get Single"};
}
However, routing is always choosing the Get single method:
$ curl -i -H "Accept: applicaiton/json" http://localhost/api/chatresponse/a/b
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Mon, 14 May 2012 18:06:26 GMT
Content-Length: 66
{"Id":0,"Template":"Get Single","Inherited":false,"Enabled":false}
$ curl -i -H "Accept: applicaiton/json" http://localhost/api/chatresponse/a/b/c
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Mon, 14 May 2012 18:06:28 GMT
Content-Length: 66
{"Id":0,"Template":"Get Single","Inherited":false,"Enabled":false}
I've tried renaming the Get all method to GetAll, as I've seen in some examples, decorating it with [HttpGet], but it still chooses the single method.
Am I completely missing something, or do I need to go about this a different way (most of the examples I see look to be related to the beta bits, and not a recent version from CodePlex)?

Try using this for the default id parameter:
new { id = System.Web.Http.RouteParameter.Optional }

Related

Unable to open "File" or "Edit" menu in Notepad using WinAppDriver

I'm trying to learn how to use WinAppDriver to test my desktop app developed in VS2019.
I'm able to run a NUnit test using the basic Notepad.exe and example of typing text. However, I'm unable to click on menu elements in Notepad for example. How do you achieve this?
Inspect.exe says its name is "Edit".
I'm using the Appium.WebDriver 4.1.1 nuget package.
Here is a good example of what I'm trying to do, but the code is outdated: https://github.com/microsoft/WinAppDriver/blob/master/Samples/C%23/NotepadTest/ScenarioMenuItem.cs
Test Code in VS2019
using NUnit.Framework;
using OpenQA.Selenium.Appium.Windows;
using System;
namespace Notepad_Tests
{
public class Tests
{
protected static WindowsDriver<WindowsElement> NotepadSession;
[SetUp]
public void Setup()
{
var appiumOptions = new OpenQA.Selenium.Appium.AppiumOptions();
appiumOptions.AddAdditionalCapability("platformName", #"Windows");
appiumOptions.AddAdditionalCapability("deviceName", #"WindowsPC");
appiumOptions.AddAdditionalCapability("app", #"C:\Windows\notepad.exe");
appiumOptions.AddAdditionalCapability("app", #"C:\Windows\notepad.exe");
appiumOptions.AddAdditionalCapability("appArguments", #"MyTestFile.txt");
appiumOptions.AddAdditionalCapability("appWorkingDir", #"D:\MyTestFolder\");
NotepadSession = new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), appiumOptions);
}
[Test]
public void Test1()
{
NotepadSession.FindElementByClassName("Edit").SendKeys("This is some text");
NotepadSession.FindElementByName("Edit").Click();
}
[TearDown]
public void TestCleanup()
{
// NotepadSession.Quit();
}
}
}
Error in WinAppDriver.exe window
==========================================
POST /session HTTP/1.1
Accept: application/json, image/png
Connection: Keep-Alive
Content-Length: 236
Content-Type: application/json; charset=utf-8
Host: 127.0.0.1:4723
User-Agent: selenium/3.141.0 (.net windows)
{"desiredCapabilities":{"platformName":"Windows","deviceName":"WindowsPC","app":"C:\\Windows\\notepad.exe","appArguments":"MyTestFile.txt","appWorkingDir":"D:\\MyTestFolder\\"},"capabilities":{"firstMatch":[{"platformName":"Windows"}]}}
HTTP/1.1 200 OK
Content-Length: 200
Content-Type: application/json
{"sessionId":"431F0707-71BD-4645-B085-5BD750FC2067","status":0,"value":{"app":"C:\\Windows\\notepad.exe","appArguments":"MyTestFile.txt","appWorkingDir":"D:\\MyTestFolder\\","platformName":"Windows"}}
==========================================
POST /session/431F0707-71BD-4645-B085-5BD750FC2067/element HTTP/1.1
Accept: application/json, image/png
Connection: Keep-Alive
Content-Length: 37
Content-Type: application/json; charset=utf-8
Host: 127.0.0.1:4723
User-Agent: selenium/3.141.0 (.net windows)
{"using":"class name","value":"Edit"}
HTTP/1.1 200 OK
Content-Length: 96
Content-Type: application/json
{"sessionId":"431F0707-71BD-4645-B085-5BD750FC2067","status":0,"value":{"ELEMENT":"42.2099874"}}
==========================================
POST /session/431F0707-71BD-4645-B085-5BD750FC2067/element/42.2099874/value HTTP/1.1
Accept: application/json, image/png
Connection: Keep-Alive
Content-Length: 31
Content-Type: application/json; charset=utf-8
Host: 127.0.0.1:4723
User-Agent: selenium/3.141.0 (.net windows)
{"value":["This is some text"]}
HTTP/1.1 200 OK
Content-Length: 63
Content-Type: application/json
{"sessionId":"431F0707-71BD-4645-B085-5BD750FC2067","status":0}
==========================================
POST /session/431F0707-71BD-4645-B085-5BD750FC2067/element HTTP/1.1
Accept: application/json, image/png
Connection: Keep-Alive
Content-Length: 31
Content-Type: application/json; charset=utf-8
Host: 127.0.0.1:4723
User-Agent: selenium/3.141.0 (.net windows)
{"using":"name","value":"Edit"}
HTTP/1.1 200 OK
Content-Length: 128
Content-Type: application/json
{"sessionId":"431F0707-71BD-4645-B085-5BD750FC2067","status":0,"value":{"ELEMENT":"42.3148312.3.-2147483646.27904.691604525.2"}}
==========================================
POST /session/431F0707-71BD-4645-B085-5BD750FC2067/element/42.3148312.3.-2147483646.27904.691604525.2/click HTTP/1.1
Accept: application/json, image/png
Connection: Keep-Alive
Content-Length: 2
Content-Type: application/json; charset=utf-8
Host: 127.0.0.1:4723
User-Agent: selenium/3.141.0 (.net windows)
{}
HTTP/1.1 400 Bad Request
Content-Length: 175
Content-Type: application/json
{"status":105,"value":{"error":"element not interactable","message":"An element command could not be completed because the element is not pointer- or keyboard interactable."}}
I got it to work by using the Microsoft.WinAppDriver.Appium.WebDriver Prerlease nugget package instead of the Appium.WebDriver 4.1.1 nuget package.
Then, the example here works perfectly:
https://github.com/microsoft/WinAppDriver/blob/master/Samples/C%23/NotepadTest/ScenarioMenuItem.cs

How do I make the default response Content-type: application/json with Scalatra

I'm learning Scalatra and am wondering how I can make the default Content-Type of my responses application/json. The current default appears to be text/html. Neat, but not really useful to my application.
The current default is text/html.
$ curl -i -X GET 'http://localhost:8080/v1/example'
HTTP/1.1 200 OK
Date: Fri, 19 Apr 2019 07:21:21 GMT
Content-Type: text/html;charset=utf-8
Content-Length: 23
Server: Jetty(9.4.8.v20171121)
HelloWorld(hello,world)
I can get application-json explicitly through the Accepted: application/json header.
$ curl -i -X GET 'http://localhost:8080/v1/example' -H 'Accept: application/json'
HTTP/1.1 200 OK
Date: Fri, 19 Apr 2019 07:22:09 GMT
Content-Type: application/json;charset=utf-8
Transfer-Encoding: chunked
Server: Jetty(9.4.8.v20171121)
{"hello":"hello","world":"world"}
How do I set the default to be application/json.
defaultFormat could be overridden to make application/json the default case. For example,
import java.io.File
import org.scalatra._
import org.scalatra.util.MimeTypes
import org.json4s.{DefaultFormats, Formats}
import org.scalatra.json._
case class HelloWorld(hello: String, world: String)
class MyScalatraServlet extends ScalatraServlet with JacksonJsonSupport {
protected implicit lazy val jsonFormats: Formats = DefaultFormats
override def defaultFormat: Symbol = 'json
get("/v1/example") {
HelloWorld("hello", "world")
}
}
without specifying Accept header should respond with
curl -i -X GET 'http://localhost:8080/v1/example'
HTTP/1.1 200 OK
Date: Thu, 25 Apr 2019 22:28:37 GMT
Content-Type: application/json;charset=utf-8
Content-Length: 33
Server: Jetty(9.4.8.v20171121)
{"hello":"hello","world":"world"}
whilst explicitly requesting Accept: text/html also works
curl -i -X GET 'http://localhost:8080/v1/example' -H 'Accept: text/html'
HTTP/1.1 200 OK
Date: Fri, 26 Apr 2019 15:45:22 GMT
Content-Type: text/html;charset=utf-8
Content-Length: 23
Server: Jetty(9.4.8.v20171121)
HelloWorld(hello,world)

HTTP OPTIONS in Xamarin Forms

Getting "204 Status Code" as No Content
Here is a simple example of OPTIONS request:
var httpClient = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Options, new Uri("http://myapi.com"));
var response = await httpClient.SendAsync(request);
Just wondering why should you need to make one? OPTIONS is used to identify allowed request methods:
To find out which request methods a server supports, one can use curl
and issue an OPTIONS request:
curl -X OPTIONS http://example.org -i
The response then contains an Allow header with the allowed methods:
HTTP/1.1 200 OK
Allow: OPTIONS, GET, HEAD, POST
Cache-Control: max-age=604800
Date: Thu, 13 Oct 2016 11:45:00 GMT
Expires: Thu, 20 Oct 2016 11:45:00 GMT
Server: EOS (lax004/2813)
x-ec-custom-error: 1
Content-Length: 0
Source: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS

spring cloud zuul: How can return service httpstatus(or handle zuul status return)?

If use zuul routes for eureka service id,service return some httpStatus like 503
curl 10.1.0.21:5701/usr/users/xxx/a -H "Api-Version: v10" -i
Returns:
HTTP/1.1 503 Service Unavailable
Set-Cookie: Session-Token=9e66fb56-21e2-457a-a00f-f788c5ce820b; path=/; domain=.0.21:5701; HttpOnly; Max-Age=2592000; Expires=Sat, 29-Oct-2016 09:23:41 GMT
Date: Thu, 29 Sep 2016 09:23:41 GMT
Session-Token-Expires: 2592000
Connection: keep-alive
ETag:
Session-Token: 9e66fb56-21e2-457a-a00f-f788c5ce820b
Transfer-Encoding: chunked
Content-Type: application/json;charset=UTF-8
X-Application-Context: user-api-provider:5701
Content-Language: en-
Access-Control-Max-Age: 1728000
[{"key":"serviceUnavailable","message":"无效的接口"}]
But zuul reuturns ok/200, why? How can I change how Zuul handles statuses?
curl 10.1.0.19:8090/usr/users/xxx/a -H "Api-Version: v10" -i
Returns:
HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 0
X-Application-Context: zuul-server:8090
Date: Thu, 29 Sep 2016 09:24:10 GMT
Works for me with latest spring-cloud-netflix (1.2.0):
#SpringBootApplication
#EnableZuulProxy
public class SimpleApplication {
public static void main(String[] args) {
SpringApplication.run(SimpleApplication.class, args);
}
}
with application.properties:
zuul.routes.test.url=http://httpstat.us/503
zuul.routes.test.path=/test/**
and:
$ curl localhost:8080/test/
> GET /test/ HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost:8080
> Accept: */*
>
< HTTP/1.1 503
< X-Application-Context: application
< Cache-Control: private
< X-AspNetMvc-Version: 5.1
< X-AspNet-Version: 4.0.30319
< X-Powered-By: ASP.NET
< Access-Control-Allow-Origin: *
< Date: Thu, 29 Sep 2016 10:24:16 GMT
< Content-Type: text/plain;charset=utf-8
< Transfer-Encoding: chunked
< Connection: close
<
* Closing connection 0
503 Service Unavailable$

add nickname to google group using api

I've been trying to use the directory API to add an alias to a group, and it doesn't seems to work, is this feature operational already?
Thank you!
Yes, it works properly. Here's an example of GAM 2.995 creating a domain alias for your reference. I'd need to see code to determine where you're having issues.
$ touch gam/debug.gam
$ gam create alias test-alias#jay.powerposters.org group testme#jay.powerposters.org
Creating alias test-alias#jay.powerposters.org for group testme#jay.powerposters.org
connect: (www.googleapis.com, 443)
send: 'POST /admin/directory/v1/groups/testme#jay.powerposters.org/aliases?alt=json HTTP/1.1
Host: www.googleapis.com
content-length: 44
accept-encoding: gzip, deflate
accept: application/json
user-agent: Google Apps Manager 2.995 / jay#ditoweb.com (Jay Lee)
/ Python 2.7.4 final
/ Linux-3.8.0-26-generic-x86_64-with-Ubuntu-13.04-raring x86_64
/ google-api-python-client/1.1
content-type: application/json
authorization: Bearer ya29.XXX
{"alias": "test-alias#jay.powerposters.org"}'
reply: 'HTTP/1.1 200 OK\r\n'
header: Cache-Control: no-cache, no-store, max-age=0, must-revalidate
header: Pragma: no-cache
header: Expires: Fri, 01 Jan 1990 00:00:00 GMT
header: Date: Wed, 10 Jul 2013 17:34:38 GMT
header: ETag: "2rnFeCLM59Q4Hv06VzEjKQtYAxk/uYyLVNg9ntD0jUUkkana74HyFfQ"
header: Content-Type: application/json; charset=UTF-8
header: Content-Encoding: gzip
header: X-Content-Type-Options: nosniff
header: X-Frame-Options: SAMEORIGIN
header: X-XSS-Protection: 1; mode=block
header: Content-Length: 112
header: Server: GSE