Testing REST Web Services

84
Testing REST Web Services SOA Symposium Berlin 2010 Jan Algermissen, [email protected]

Transcript of Testing REST Web Services

Page 1: Testing REST Web Services

Testing RESTWeb Services

SOA Symposium Berlin 2010Jan Algermissen, [email protected]

Page 2: Testing REST Web Services

Jan Algermissen

@algermissen

http://www.nordsc.com/

“Applying the Web to Enterprise IT”

Page 3: Testing REST Web Services

MORE TO TEST THAN YOU THINK!

Page 4: Testing REST Web Services

Client Server

Communication requires shared knowledge.

Page 5: Testing REST Web Services

Client Server

Shared Knowledge Allows Expectations

Page 6: Testing REST Web Services

Client Server

Expectation Levels

Page 7: Testing REST Web Services

Expectation Levels

Page 8: Testing REST Web Services

Message

Expectation Levels

Page 9: Testing REST Web Services

Resource

Message

Expectation Levels

Page 10: Testing REST Web Services

Application

Resource

Message

Expectation Levels

Page 11: Testing REST Web Services

MESSAGE LEVEL FAULTS

Page 12: Testing REST Web Services

GET /index.html HTTP/1.0

Page 13: Testing REST Web Services

?

GET /index.html HTTP/1.0

HTTP/1.0 204 No Content/* Hello there */

Page 14: Testing REST Web Services

DELETE /index.html HTTP/1.0

Page 15: Testing REST Web Services

DELETE /index.html HTTP/1.0

HTTP/1.0 404.6 Verb deniedContent-Type: text/plain

Sorry, you cannot delete this resource.

?

Page 16: Testing REST Web Services

GET /index.html HTTP/1.0

Page 17: Testing REST Web Services

GET /index.html HTTP/1.0

HTTP/1.0 305 Use Proxy

?

Page 18: Testing REST Web Services

HTTP 1.1(RFC 2616)

Page 19: Testing REST Web Services

GET /news HTTP/1.0

Page 20: Testing REST Web Services

GET /news HTTP/1.0

HTTP/1.0 200 OkContent-Type: application/atom+xml

<feed> <entry></fee>

?

Page 21: Testing REST Web Services

GET /news HTTP/1.0

Page 22: Testing REST Web Services

GET /news HTTP/1.0

HTTP/1.0 200 OkContent-Type: application/atom+xml

<feed> <link href=”/de/news” rel=”alternate” hreflang=”de”/> <link href=”/de/news2” rel=”alternate” hreflang=”de”/></feed>

?

Page 23: Testing REST Web Services

MEDIA TYPE

Page 24: Testing REST Web Services

TESTS

MessageLevel

•Correct HTTP syntax?•Correct HTTP semantics?•Correct payload syntax?•Correct payload semantics?

Page 25: Testing REST Web Services

Message leveltests passed!

Are we done testing?

Page 26: Testing REST Web Services

“The notion that REST-based user agents can't have expectations of a resource is clearly false.”

-- Roy Fielding

Page 27: Testing REST Web Services

GREAT!MORE TO TEST

Page 28: Testing REST Web Services

RESOURCE LEVEL FAULTS

Page 29: Testing REST Web Services

GET /page.html HTTP/1.0

Page 30: Testing REST Web Services

GET /page.html HTTP/1.0

200 OkContent-Type: text/html

<html> <body><p>My dog: <img src=”/img/dog.jpg”/> </p></body></html>

Page 31: Testing REST Web Services

GET /img/dog.jpg HTTP/1.0Accept: image/*

Page 32: Testing REST Web Services

GET /img/dog.jpg HTTP/1.0Accept: image/*

HTTP/1.0 406 Not AcceptableContent-Length: 0

Page 33: Testing REST Web Services

Link semantics create expectations.

Page 34: Testing REST Web Services

<html><head> <link rel=”stylesheet” type=”text/css” href=”/css/default.css”/> </head>....</html>

“A stylesheet”

Page 35: Testing REST Web Services

<app:collection href=”/blog/entries”> <app:accept>text/plain</app:accept></app:collection>

“A collection”

Page 36: Testing REST Web Services

<OpenSearchDescription> <Url type=”application/rss+xml” template=”/search?q={searchTerms}”/></OpenSearchDescription>

“A search result”

Page 37: Testing REST Web Services

Link semantics create expectations.

These expectations can be tested.

Page 38: Testing REST Web Services

BUT HOW?

Page 39: Testing REST Web Services

Specifications (loosely) associate link semantics

with media types.

Page 40: Testing REST Web Services

curl -I http://foo.org/img/dog.jpg -HAccept:image/*

HTTP/1.1 406 Not AcceptableContent-Length: 0

Verify that it is an image

406 —› Test failed

Page 41: Testing REST Web Services

curl -I http://foo.org/blog/entries \ -HAccept:application/atom+xml

HTTP/1.1 200 OkContent-Type: application/atom+xml

Verify that it is a collection

200 —› Test passed

Page 42: Testing REST Web Services

Use media types to test resource

semantics

Page 43: Testing REST Web Services

GET /articles/1002110.html HTTP/1.0

Page 44: Testing REST Web Services

GET /articles/1002110.html HTTP/1.0

HTTP/1.0 200 OkDate: Tue, 2 Jun 2010 11:00:00 GMTContent-Type: text/html

<html>...</html>

Page 45: Testing REST Web Services

GET /articles/1002110.html HTTP/1.0

Page 46: Testing REST Web Services

GET /articles/1002110.html HTTP/1.0

HTTP/1.0 404 Not FoundDate: Wed, 3 Jun 2010 11:00:00 GMTContent-Type: text/plain

Nothing found that matches the request URI.

?

Page 47: Testing REST Web Services

GET /stock-quote/COKE HTTP/1.0

Page 48: Testing REST Web Services

GET /stock-quote/COKE HTTP/1.0

HTTP/1.0 200 OkDate: Tue, 2 Jun 2010 11:00:00 GMTContent-Type: text/plain

49.5

Page 49: Testing REST Web Services

GET /stock-quote/COKE HTTP/1.0

Page 50: Testing REST Web Services

GET /stock-quote/COKE HTTP/1.0

HTTP/1.0 200 OkDate: Wed, 3 Jun 2010 11:00:00 GMTContent-Type: text/plain

Current weather in Berlin: 25 degrees Celsius, sunny.

?

Page 51: Testing REST Web Services

Cool URIs don’t change!

Page 52: Testing REST Web Services

GET /orders/42 HTTP/1.0Accept: application/order

Page 53: Testing REST Web Services

GET /orders/42 HTTP/1.0Accept: application/order

HTTP/1.0 200 OkDate: Tue, 2 Jun 2010 11:00:00 GMTContent-Type: application/order

<order> ... </order>

Page 54: Testing REST Web Services

GET /orders/42 HTTP/1.0Accept: application/order

Page 55: Testing REST Web Services

GET /orders/42 HTTP/1.0Accept: application/order

HTTP/1.0 406 Not AcceptableDate: Wed, 3 Jun 2010 11:00:00 GMTContent-Type: text/plain

Media type application/order no longer available. Try application/order.v2

?

Page 56: Testing REST Web Services

BE NICE!

Keep variants around.

Page 57: Testing REST Web Services

TESTS

ResourceLevel

•Does resource match link semantics?•Is resource available over time?•Are resource semantics stable over time?•Are variants available over time?

Page 58: Testing REST Web Services

APPLICATION LEVEL FAULTS

Page 59: Testing REST Web Services

What’s an application anyway?

•Buying a book in an online store•Looking up a word in an online dictionary•Taking an online exam•Indexing a set of Web sites•Checking for updates in monitored feeds•Monitoring programming language

adoption using stackoverflow.com

Page 60: Testing REST Web Services

GET /shop-home.html HTTP/1.0Host: www.bestbookshop.com

Page 61: Testing REST Web Services

GET /shop-home.html HTTP/1.0Host: www.bestbookshop.com

HTTP/1.0 200 OkContent-Type: text/html

<html><body><p>Sorry, buying is not possible here anymore. Check out the store catalog at <a href=”/catalog.html”/> and walk to one of our stores. </p></body></html>

?

Page 62: Testing REST Web Services

Application Level Tests

Does the service workas expected by the application?

Page 63: Testing REST Web Services

Application Level Tests

Does the service workas expected by the application?

Testable only in context of the application. The server cannot know how it is being re-used!

Page 64: Testing REST Web Services

TESTS

ApplicationLevel

•Does service offer expected capability?•Is the user goal reachable?

Page 65: Testing REST Web Services

Testing REST Web Services

Page 66: Testing REST Web Services

Testing REST Web Services

MessageLevel

•Correct HTTP syntax?•Correct HTTP semantics?•Correct payload syntax?•Correct payload semantics?

Page 67: Testing REST Web Services

ResourceLevel

•Does resource match link semantics?•Is resource available over time?•Are resource semantics stable over time?•Are variants available over time?

Testing REST Web Services

MessageLevel

•Correct HTTP syntax?•Correct HTTP semantics?•Correct payload syntax?•Correct payload semantics?

Page 68: Testing REST Web Services

ResourceLevel

•Does resource match link semantics?•Is resource available over time?•Are resource semantics stable over time?•Are variants available over time?

Testing REST Web Services

ApplicationLevel

•Does service offer expected capability?•Is the user goal reachable?

MessageLevel

•Correct HTTP syntax?•Correct HTTP semantics?•Correct payload syntax?•Correct payload semantics?

Page 69: Testing REST Web Services

EXPLORE THECONTRACT

Page 70: Testing REST Web Services

SERVER MUST PASS TESTS

Page 71: Testing REST Web Services

SERVER MUST PASS TESTS

NO CLIENT ASSUMPTIONS BEYOND TESTS

Page 72: Testing REST Web Services

USETESTS ASGUIDE

Server Developer Client Developer

Page 73: Testing REST Web Services

Server Developer Guidance

Page 74: Testing REST Web Services

Server must...

•Conform to HTTP•Conform to the media types used•Resources must match link semantics•Keep URIs around•Keep resource semantics stable•Maintain variants over time•Maintain capabilities

Page 75: Testing REST Web Services

Anything else is not constrained and will

not break clients!

Page 76: Testing REST Web Services

Server may....

•Change representations•Add resources•Add variants•Make full use of HTTP•Make full use of media types

Page 77: Testing REST Web Services

Client Developer Guidance

Page 78: Testing REST Web Services

Client may rely on...

•Message level correctness•Resources matching link semantics•Bookmarkable URIs•Stable resource semantics•Variant availability over time•Stable service nature

Page 79: Testing REST Web Services

Anything else the server is absolutely

free to change at any time.

Page 80: Testing REST Web Services

The client must...

•Implement all of HTTP•Deal with supported media types

completely•Prepare for unexpected but valid

responses

Page 81: Testing REST Web Services

Take Aways

•Quite some things to test•Tests reveal and illustrate contract•REST constrains over time•Servers are, in fact, rather constrained

Page 82: Testing REST Web Services

REST achieves evolvability by

rigorous constraints on server change!

Page 83: Testing REST Web Services

HAPPY TESTING!

Page 84: Testing REST Web Services